-
Notifications
You must be signed in to change notification settings - Fork 0
/
easy_merge_lists.py
66 lines (54 loc) · 1.58 KB
/
easy_merge_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
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def __init__(self):
self.head = None
def insert(self, new):
if not self.head:
self.head = ListNode(new.val)
return
node = self.head
while node.next:
node = node.next
node.next = ListNode(new.val)
def mergeTwoLists(self, list1: list, list2: list) -> list:
item1, item2 = list1, list2
while item1 or item2:
if not item1:
self.insert(item2)
item2 = item2.next
elif not item2:
self.insert(item1)
item1 = item1.next
elif item1.val >= item2.val:
self.insert(item2)
item2 = item2.next
else:
self.insert(item1)
item1 = item1.next
return self.head
class LinkedList:
def __init__(self, input):
self.head = None
self.input = input
def insert(self, new):
node = self.head
while node.next:
node = node.next
node.next = ListNode(val=new)
def build_list(self):
self.head = ListNode(val=self.input[0])
for item in self.input[1:]:
self.insert(item)
list1 = [1,2,4]
list2 = [1,3,4]
linklist1 = LinkedList(list1)
linklist1.build_list()
linklist2 = LinkedList(list2)
linklist2.build_list()
sol = Solution()
sol.mergeTwoLists(linklist1.head, linklist2.head)
print(sol.head)