假如我现在有两个链表 A, B
A 链表: 1 -> 3 -> 5
B 链表: 2 -> 4 -> 6
最终合并结果是 链表: 1 -> 2 -> 3 -> 4 -> 5 -> 6 该如何做?
1
frandy 2018-09-06 18:26:42 +08:00
a = [1,3,5]
b = [2,4,6] c = a+b c.sort() print(c) |
2
meik2333 2018-09-06 18:33:46 +08:00
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (l1 == nullptr) { return l2; } else if (l2 == nullptr) { return l1; } auto *head = new ListNode(0); auto *cur = head; while (l1 and l2) { if (l1->val < l2->val) { cur->next = l1; l1 = l1->next; } else { cur->next = l2; l2 = l2->next; } cur = cur->next; } cur->next = l1 ? l1 : l2; cur = head->next; delete head; return cur; } |
4
ihainan 2018-09-06 18:38:25 +08:00 via iPhone
|
5
dbw9580 2018-09-06 18:53:07 +08:00 via Android
list(itertools.chain.from_iterable(zip(a,b)))
|
6
zyp0921 2018-09-06 18:54:02 +08:00
比大小呗- -
|
7
anonymous256 2018-09-06 18:57:03 +08:00 via Android
c = [*a, *b]
c.sort() print(c) |
8
seven2016 2018-09-06 19:01:51 +08:00
|
9
someonedeng 2018-09-06 21:25:52 +08:00 2
作业要自己做。
|
10
stargazer 2018-09-06 22:47:06 +08:00
合并有序链表,,,
|
11
tt67wq 2018-09-07 10:09:35 +08:00
归并排序似乎就是这个
|
12
Cukuyo 2018-09-07 14:16:55 +08:00
作业要自己做
|