-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.py
More file actions
150 lines (135 loc) · 3.89 KB
/
linkedlist.py
File metadata and controls
150 lines (135 loc) · 3.89 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from error import error
class node(object):
def __init__(self, value, next=None):
self.value = value
self.next = next
class linked_list(object):
def __init__(self):
self.head = None
self.tail = None
self.size = -1
#Add at the end of linked list in O(1)
def add(self, value):
newNode = node(value)
if not self.tail:
self.head = newNode
else:
self.tail.next = newNode
self.tail = newNode
self.size+=1
def addAtHead(self,value):
newNode = node(value)
if not self.head:
self.head = newNode
else:
current = self.head
self.head = newNode
self.head.next = current
def addAtIndex(self,value,index):
newNode = node(value)
current = self.head
while index != 1:
index -=1
if current :
current = current.next
nextNode = current.next
current.next = newNode
newNode.next = nextNode
self.size +=1
def get(self, index):
current = self.head
while index:
if not current:
return None
index -= 1
current = current.next
return current.value
def delete(self, index):
current = self.head
previous = None
if index < 0:
return error("Operation not supported")
elif index is 0:
self.head = current.next
value = current.value
current.next = None
self.size-=1
return value
else:
while index:
if not current:
return error("Out of bounds")
index -= 1
previous = current
current = current.next
previous.next = current.next
value = current.value
current.next = None
self.size-=1
return value
def get_size(self):
return self.size
def print_list(self):
current = self.head
while current:
print(current.value, end='->')
current = current.next
print()
def reverse(self):
current = self.head
prev = None
next = None
if self.head:
if self.head.next:
next = current.next
self.head.next = None
while next:
prev = current
current = next
next = current.next
current.next = prev
self.head = current
#pass head of two lists to compare
def compare_lists(llist1, llist2):
list1_current = llist1
list2_current = llist2
while list1_current and list2_current:
if list1_current.value != list2_current.value:
return 0
list1_current =list1_current.next
list2_current = list2_current.next
if list1_current != list2_current:
return 0
return 1
#merge two sorted linked list
def mergeLists(head1, head2):
current1 = head1
current2 = head2
if not head1:
return head2
if not head2:
return head1
head = None
current = None
while current1 and current2:
if current1.value < current2.value:
if not head:
head = node(current1.value)
current = head
else:
current.next = node(current1.value)
current = current.next
current1 = current1.next
else:
if not head:
head = node(current2.value)
current = head
else:
current.next = node(current2.value)
current = current.next
current2 = current2.next
if not current1:
current.next = current2
if not current2:
current.next = current1
return head