leetcode | Merge Two Sorted Lists
2015-05-13 10:55
477 查看
Merge Two Sorted Lists
C实现
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.
C实现
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ //时间复杂度O(min(m, n)) 空间复杂度O(1) struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { if (l1 == NULL || l2 == NULL) return (l1 == NULL) ? l2 : l1; struct ListNode* dummy = (struct ListNode*)malloc(sizeof(*dummy));//假节点 struct ListNode* prev = dummy; while (l1 && l2) { if (l1->val < l2->val) { prev->next = l1; l1 = l1->next; } else { prev->next = l2; l2 = l2->next; } prev = prev->next; } prev->next = (l1 == NULL) ? l2 : l1; return dummy->next; }
相关文章推荐
- 【leetcode 链表】 Merge Two Sorted Lists 和 Merge k Sorted Lists
- LeetCode 21: Merge Two Sorted Lists
- LeetCode Merge Two Sorted Lists
- LeetCode: Merge Two Sorted Lists
- [leetcode刷题系列]Merge Two Sorted Lists
- LeetCode之Merge Two Sorted Lists
- leetcode:21 Merge Two Sorted Lists-每日编程第十七题
- 【LeetCode】LeetCode——第21题:Merge Two Sorted Lists
- [LeetCode] Merge Two Sorted Lists
- leetcode_021 Merge Two Sorted Lists
- 【LeetCode】【Linked List】Merge Two Sorted Lists
- 【LeetCode-21】 Merge Two Sorted Lists(C++)
- [LeetCode]Merge Two Sorted Lists@python
- LeetCode Merge Two Sorted Lists 归并排序
- leetcode 21. Merge Two Sorted Lists(C语言,合并两个排序的链表为一个排序链表)32
- leetcode-Merge Two Sorted Lists
- LeetCode_Linked List_Merge Two Sorted Lists
- LeetCode: Merge Two Sorted Lists
- LeetCode 21 Merge Two Sorted Lists(合并两个已排序的链表)(Linked List)
- LeetCode:21_Merge Two Sorted Lists | 合并两个排序列表 | Easy