-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21. 合并两个有序链表.py
63 lines (54 loc) · 1.6 KB
/
21. 合并两个有序链表.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'''
Description: 合并两个有序链表
Author: AskeyNil
CreateDate: 2019-09-07 21:53:02
LastEditors: AskeyNil
********************************
** **
** It works on my machine **
** **
********************************
'''
"""
' 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给
' 定的两个链表的所有节点组成的。
'
' 示例:
' 输入:1->2->4, 1->3->4
' 输出:1->1->2->3->4->4
'
"""
# ! ###################### START ##########################
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if not l1 or not l2:
return l1 if l1 else l2
if l1.val <= l2.val:
first = l1
head = l1
second = l2
else:
first = l2
head = l2
second = l1
while head:
# 碰到大于等于的数 才要插入到前面
if head.val > second.val:
head.val, second.val = second.val, head.val
temp = head.next
head.next = second
second = second.next
head.next.next = temp
# second, head.next, second.next = second.next, second, head.next
if head.next is None:
head.next = second
break
head = head.next
if second is None:
break
return first