Skip to content

Commit 8a421fe

Browse files
committed
feat: add solutions to lcof problem: No.25
1 parent 40bf995 commit 8a421fe

File tree

8 files changed

+389
-202
lines changed

8 files changed

+389
-202
lines changed

lcof/面试题25. 合并两个排序的链表/README.md

+313-135
Large diffs are not rendered by default.

lcof/面试题25. 合并两个排序的链表/Solution.cpp

+13-18
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,22 @@
66
* ListNode(int x) : val(x), next(NULL) {}
77
* };
88
*/
9-
109
class Solution {
1110
public:
1211
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
13-
if (nullptr == l1 && nullptr == l2) {
14-
return nullptr;
12+
ListNode* dummy = new ListNode(0);
13+
ListNode* cur = dummy;
14+
while (l1 && l2) {
15+
if (l1->val <= l2->val) {
16+
cur->next = l1;
17+
l1 = l1->next;
18+
} else {
19+
cur->next = l2;
20+
l2 = l2->next;
21+
}
22+
cur = cur->next;
1523
}
16-
17-
if (nullptr == l1 || nullptr == l2) {
18-
return l1 == nullptr ? l2 : l1;
19-
}
20-
21-
ListNode* node = nullptr;
22-
if (l1->val > l2->val) {
23-
node = l2;
24-
node->next = mergeTwoLists(l1, l2->next);
25-
} else {
26-
node = l1;
27-
node->next = mergeTwoLists(l1->next, l2);
28-
}
29-
30-
return node;
24+
cur->next = l1 ? l1 : l2;
25+
return dummy->next;
3126
}
3227
};

lcof/面试题25. 合并两个排序的链表/Solution.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
*/
99
public class Solution {
1010
public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
11-
ListNode p = new ListNode();
12-
ListNode q = p;
11+
ListNode dummy = new ListNode(0);
12+
ListNode cur = dummy;
1313
while (l1 != null && l2 != null) {
14-
if (l1.val < l2.val) {
15-
p.next = l1;
14+
if (l1.val <= l2.val) {
15+
cur.next = l1;
1616
l1 = l1.next;
1717
} else {
18-
p.next = l2;
18+
cur.next = l2;
1919
l2 = l2.next;
2020
}
21-
p = p.next;
21+
cur = cur.next;
2222
}
23-
p.next = l1 == null ? l2 : l1;
24-
return q.next;
23+
cur.next = l1 == null ? l2 : l1;
24+
return dummy.next;
2525
}
2626
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
18
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
2-
if l1 == nil {
3-
return l2
4-
}
5-
if l2 == nil {
6-
return l1
7-
}
8-
if l1.Val <= l2.Val {
9-
l1.Next = mergeTwoLists(l1.Next,l2)
10-
return l1
11-
}
12-
l2.Next = mergeTwoLists(l1, l2.Next)
13-
return l2
9+
dummy := &ListNode{}
10+
cur := dummy
11+
for l1 != nil && l2 != nil {
12+
if l1.Val <= l2.Val {
13+
cur.Next = l1
14+
l1 = l1.Next
15+
} else {
16+
cur.Next = l2
17+
l2 = l2.Next
18+
}
19+
cur = cur.Next
20+
}
21+
if l1 == nil {
22+
cur.Next = l2
23+
} else {
24+
cur.Next = l1
25+
}
26+
return dummy.Next
1427
}

lcof/面试题25. 合并两个排序的链表/Solution.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
class Solution {
1010
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
1111
ListNode dummy = new ListNode(0);
12-
ListNode p = dummy;
12+
ListNode cur = dummy;
1313
while (l1 != null && l2 != null) {
1414
if (l1.val <= l2.val) {
15-
p.next = l1;
15+
cur.next = l1;
1616
l1 = l1.next;
1717
} else {
18-
p.next = l2;
18+
cur.next = l2;
1919
l2 = l2.next;
2020
}
21-
p = p.next;
21+
cur = cur.next;
2222
}
23-
p.next = l1 == null ? l2 : l1;
23+
cur.next = l1 == null ? l2 : l1;
2424
return dummy.next;
2525
}
2626
}

lcof/面试题25. 合并两个排序的链表/Solution.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
* @return {ListNode}
1212
*/
1313
var mergeTwoLists = function (l1, l2) {
14-
if (!(l1 && l2)) {
15-
return l1 || l2;
16-
}
17-
if (l1.val < l2.val) {
18-
l1.next = mergeTwoLists(l1.next, l2);
19-
return l1;
20-
} else {
21-
l2.next = mergeTwoLists(l2.next, l1);
22-
return l2;
14+
const dummy = new ListNode(0);
15+
let cur = dummy;
16+
while (l1 && l2) {
17+
if (l1.val <= l2.val) {
18+
cur.next = l1;
19+
l1 = l1.next;
20+
} else {
21+
cur.next = l2;
22+
l2 = l2.next;
23+
}
24+
cur = cur.next;
2325
}
26+
cur.next = l1 || l2;
27+
return dummy.next;
2428
};

lcof/面试题25. 合并两个排序的链表/Solution.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77

88
class Solution:
99
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
10-
dummy = ListNode(0)
11-
p = dummy
10+
dummy = cur = ListNode(0)
1211
while l1 and l2:
1312
if l1.val <= l2.val:
14-
p.next = l1
13+
cur.next = l1
1514
l1 = l1.next
1615
else:
17-
p.next = l2
16+
cur.next = l2
1817
l2 = l2.next
19-
p = p.next
20-
p.next = l1 or l2
18+
cur = cur.next
19+
cur.next = l1 or l2
2120
return dummy.next

lcof/面试题25. 合并两个排序的链表/Solution.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,18 @@ function mergeTwoLists(
1414
l1: ListNode | null,
1515
l2: ListNode | null,
1616
): ListNode | null {
17-
const duumy = new ListNode();
18-
let cur = duumy;
17+
const dummy = new ListNode(0);
18+
let cur = dummy;
1919
while (l1 && l2) {
20-
let node: ListNode;
21-
if (l1.val < l2.val) {
22-
node = l1;
20+
if (l1.val <= l2.val) {
21+
cur.next = l1;
2322
l1 = l1.next;
2423
} else {
25-
node = l2;
24+
cur.next = l2;
2625
l2 = l2.next;
2726
}
28-
cur.next = node;
29-
cur = node;
27+
cur = cur.next;
3028
}
3129
cur.next = l1 || l2;
32-
return duumy.next;
30+
return dummy.next;
3331
}

0 commit comments

Comments
 (0)