-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy path064 Merge Two Sorted Lists.py
58 lines (48 loc) · 1.5 KB
/
064 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
"""
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.
"""
__author__ = 'Danyang'
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def __repr__(self):
return repr(self.val)
def __str__(self):
return "%d, %s"%(self.val, self.next)
class Solution:
def mergeTwoLists(self, l1, l2):
"""
Linked List
assuming ascending order
:param l1: ListNode
:param l2: ListNode
:return:
"""
dummy = ListNode(0)
dummy.next = l1
pre = dummy
the_other = l2
while pre and pre.next:
cur = pre.next
if the_other and cur.val>the_other.val:
# insert
temp = the_other.next
pre.next, the_other.next = the_other, cur
the_other = temp # advance the_other
pre = pre.next
# dangling list
if the_other:
pre.next = the_other # appending
return dummy.next
if __name__=="__main__":
length = 10
list1 = [ListNode(2*i) for i in xrange(length)]
list2 = [ListNode(2*i+1) for i in xrange(length)]
for i in xrange(length-1):
list1[i].next = list1[i+1]
list2[i].next = list2[i+1]
lst = Solution().mergeTwoLists(list1[0], list2[0])
print lst