Skip to content

Commit 7c6e40f

Browse files
authored
Add files via upload
1 parent d083adc commit 7c6e40f

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

剑指Offer/16.合并两个排序的链表.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,19 @@ def Merge(self, pHead1, pHead2):
1616
else:
1717
head = pHead2
1818
head.next = self.Merge(pHead1, pHead2.next)
19-
return head
19+
return head
20+
21+
22+
class Solution:
23+
def Merge(self, pHead1, pHead2):
24+
p = q = ListNode(None)
25+
while pHead1 and pHead2:
26+
if pHead1.val < pHead2.val:
27+
p.next = pHead1
28+
pHead1 = pHead1.next
29+
else:
30+
p.next = pHead2
31+
pHead2 = pHead2.next
32+
p = p.next
33+
p.next = pHead1 or pHead2
34+
return q.next

0 commit comments

Comments
 (0)