-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge Two Sorted Lists.py
73 lines (54 loc) · 1.35 KB
/
Merge Two Sorted Lists.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
"""
Merge two sorted linked lists and return it as a new list.
The new list should be made by splicing together the nodes of the first two lists.
"""
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# @param two ListNodes
# @return a ListNode
def mergeTwoLists(self, l1, l2):
# base cases
if l1 == None: return l2
if l2 == None: return l1
# pointers to l1 and l2
p1 = l1
p2 = l2
# dummy node pointing to merged list
dummy = ListNode(-1)
p_merged = dummy
# while both lists has remaining nodes
while p1 and p2:
# merge p1 first
if p1.val <= p2.val:
p_merged.next = p1
p1 = p1.next
# meege p2 first
else:
p_merged.next = p2
p2 = p2.next
# update the merged pointer
p_merged = p_merged.next
# concateante the remaining list that haven't merged
# when the 2 list are not of the same length
if p1 == None:
p_merged.next = p2
if p2 == None:
p_merged.next = p1
return dummy.next
def printList(self, head, size=100):
pointer = head
s = ""
counter = 0
while pointer != None and counter < size:
s = s + str(pointer.val) + " , "
pointer = pointer.next
counter = counter + 1
print s
s = Solution()
p1 = ListNode(1)
p2 = ListNode(2)
s.printList(s.mergeTwoLists(p1, p2))