[LeetCode OJ]Merge Two Sorted Lists
2014-10-21 21:20
399 查看
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
感觉这道题对自己的帮助还是挺大的,对于链表和指针都有一个更好的理解。
头指针head赋给temp那一下反应了不短的时间,想明白以后觉得自己好蠢啊= =
感觉这道题对自己的帮助还是挺大的,对于链表和指针都有一个更好的理解。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode *p; ListNode *q; ListNode *temp; ListNode *head; if(l1 == NULL && l2 == NULL) return NULL; if(l1 == NULL) return l2; if(l2 == NULL) return l1; p = l1; q = l2; head = NULL; if(p->val < q->val) { head = p; p = p->next; } else { head = q; q = q->next; } temp = head; while(p && q) { if(p->val < q->val) { temp->next = p; p = p->next; } else { temp->next = q; q = q->next; } temp = temp->next; } if(p != NULL) { temp->next = p; } else { temp->next = q; } return head; } };
头指针head赋给temp那一下反应了不短的时间,想明白以后觉得自己好蠢啊= =
相关文章推荐
- 【LeetCode OJ】Merge Two Sorted Lists
- LeetCode OJ--Merge Two Sorted Lists
- LeetCode OJ:Merge Two Sorted Lists(合并两个链表)
- LeetCode OJ:Merge Two Sorted Lists
- LeetCode OJ 之Merge TwoSorted Lists 解题报告
- LeetCode OJ——Merge Two Sorted Lists
- LeetCode OJ 之 Merge Two Sorted Lists(合并两个有序的链表)
- LeetCode OJ - Merge Sorted Array & Merge Two Sorted Lists
- LeetCode OJ - Merge Two Sorted Lists
- 【LeetCode OJ】Merge Two Sorted Lists
- 21. Merge Two Sorted Lists
- LeetCode 21: Merge Two Sorted Lists
- LeetCode 21. Merge Two Sorted Lists(C++版)
- [LeetCode]Merge Two Sorted Lists
- LeetCode_Merge Two Sorted Lists
- [LeetCode 21]Merge Two Sorted Lists 合并两个有序链表
- LeetCode:Merge Two Sorted Lists
- LeetCode - Merge Two Sorted Lists
- 21. Merge Two Sorted Lists(第七周)
- LeetCode 21. Merge Two Sorted Lists