Skip to content

Commit a2a0c2c

Browse files
committed
feat: add python and java solution to lcof problem
添加《剑指Offer》题解:面试题25. 合并两个排序的链表
1 parent b685daf commit a2a0c2c

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# [面试题25. 合并两个排序的链表](https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/)
2+
3+
## 题目描述
4+
输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。
5+
6+
**示例1:**
7+
8+
```
9+
输入:1->2->4, 1->3->4
10+
输出:1->1->2->3->4->4
11+
```
12+
13+
**限制:**
14+
15+
- `0 <= 链表长度 <= 1000`
16+
17+
## 解法
18+
### Python3
19+
```python
20+
# Definition for singly-linked list.
21+
# class ListNode:
22+
# def __init__(self, x):
23+
# self.val = x
24+
# self.next = None
25+
26+
class Solution:
27+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
28+
if l1 is None or l2 is None:
29+
return l1 or l2
30+
head = ListNode(0)
31+
p = head
32+
while l1 and l2:
33+
if l1.val < l2.val:
34+
t = l1.next
35+
p.next = l1
36+
p = l1
37+
l1 = t
38+
else:
39+
t = l2.next
40+
p.next = l2
41+
p = l2
42+
l2 = t
43+
44+
p.next = l1 or l2
45+
return head.next
46+
```
47+
48+
### Java
49+
```java
50+
/**
51+
* Definition for singly-linked list.
52+
* public class ListNode {
53+
* int val;
54+
* ListNode next;
55+
* ListNode(int x) { val = x; }
56+
* }
57+
*/
58+
class Solution {
59+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
60+
if (l1 == null) {
61+
return l2;
62+
}
63+
if (l2 == null) {
64+
return l1;
65+
}
66+
67+
ListNode head = new ListNode(0);
68+
ListNode p = head;
69+
while (l1 != null && l2 != null) {
70+
if (l1.val < l2.val) {
71+
ListNode t = l1.next;
72+
p.next = l1;
73+
p = l1;
74+
l1 = t;
75+
} else {
76+
ListNode t = l2.next;
77+
p.next = l2;
78+
p = l2;
79+
l2 = t;
80+
}
81+
}
82+
p.next = l1 == null ? l2 : l1;
83+
return head.next;
84+
}
85+
}
86+
```
87+
88+
### ...
89+
```
90+
91+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
11+
if (l1 == null) {
12+
return l2;
13+
}
14+
if (l2 == null) {
15+
return l1;
16+
}
17+
18+
ListNode head = new ListNode(0);
19+
ListNode p = head;
20+
while (l1 != null && l2 != null) {
21+
if (l1.val < l2.val) {
22+
ListNode t = l1.next;
23+
p.next = l1;
24+
p = l1;
25+
l1 = t;
26+
} else {
27+
ListNode t = l2.next;
28+
p.next = l2;
29+
p = l2;
30+
l2 = t;
31+
}
32+
}
33+
p.next = l1 == null ? l2 : l1;
34+
return head.next;
35+
}
36+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
9+
if l1 is None or l2 is None:
10+
return l1 or l2
11+
head = ListNode(0)
12+
p = head
13+
while l1 and l2:
14+
if l1.val < l2.val:
15+
t = l1.next
16+
p.next = l1
17+
p = l1
18+
l1 = t
19+
else:
20+
t = l2.next
21+
p.next = l2
22+
p = l2
23+
l2 = t
24+
25+
p.next = l1 or l2
26+
return head.next

0 commit comments

Comments
 (0)