-
Notifications
You must be signed in to change notification settings - Fork 0
/
21.合并两个有序链表.py
87 lines (72 loc) · 2.11 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
合并两个有序链表:
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
"""
"""
解题思路:
归并排序的归并思想,将两个链表逐个元素比较大小,较小的先加入新链表,直至其中一个为空,把另一个链表的全部元素加入即可。
"""
# 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:
res=ListNode(None)
Node=res
while l1 and l2:
if l1.val<l2.val:
Node.next,l1=l1,l1.next
else:
Node.next,l2=l2,l2.next
Node=Node.next
if l1:
Node.next=l1
if l2:
Node.next=l2
return res.next
def stringToIntegerList(input):
return json.loads(input)
def stringToListNode(input):
# Generate list from the input
numbers = stringToIntegerList(input)
# Now convert that list into linked list
dummyRoot = ListNode(0)
ptr = dummyRoot
for number in numbers:
ptr.next = ListNode(number)
ptr = ptr.next
ptr = dummyRoot.next
return ptr
def listNodeToString(node):
if not node:
return "[]"
result = ""
while node:
result += str(node.val) + ", "
node = node.next
return "[" + result[:-2] + "]"
def main():
import sys
import io
def readlines():
for line in io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8'):
yield line.strip('\n')
lines = readlines()
while True:
try:
line = next(lines)
l1 = stringToListNode(line);
line = next(lines)
l2 = stringToListNode(line);
ret = Solution().mergeTwoLists(l1, l2)
out = listNodeToString(ret);
print(out)
except StopIteration:
break
if __name__ == '__main__':
main()