-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21_Merge_Two_Sorted_Lists.py
More file actions
78 lines (73 loc) · 1.78 KB
/
21_Merge_Two_Sorted_Lists.py
File metadata and controls
78 lines (73 loc) · 1.78 KB
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
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def mergeTwoLists_backup(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1 is None:
return l2
if l2 is None:
return l1
l = []
while(l1):
l.append(l1.val)
l1 = l1.next
while(l2):
l.append(l2.val)
l2 = l2.next
l.sort()
aa = ListNode(l[0])
point = aa
for i in l[1:]:
node = ListNode(i)
point.next = node
point = point.next
return aa
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1 is None:
return l2
elif l2 is None:
return l1
p1 = l1
p2 = l2
head = ListNode(-1)
p = head
while p1 and p2:
if p1.val <= p2.val:
pp = ListNode(p1.val)
p.next = pp
p1 = p1.next
elif p1.val > p2.val:
pp = ListNode(p2.val)
p.next = pp
p2 = p2.next
p = p.next
if p1 is None:
p.next = p2
elif p2 is None:
p.next = p1
head = head.next
while(head):
print head.val
head = head.next
return head
if __name__ == '__main__':
a = Solution()
b1 = ListNode(2)
#b1.next = ListNode(2)
#b1.next.next = ListNode(3)
b2 = ListNode(1)
#b2.next = ListNode(3)
#b2.next.next = ListNode(4)
a.mergeTwoLists(b1, b2)