-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglyLinkedList.py
195 lines (176 loc) · 5.3 KB
/
SinglyLinkedList.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
class Node:
def __init__(self, e, n):
self.element = e
self.next = n
class LinkedList:
def __init__(self, a):
if type(a) == list:
self.head = Node(a[0], None)
tail = self.head
i = 1
while i < len(a):
newNode = Node(a[i], None)
tail.next = newNode
tail = newNode
i += 1
else:
self.head = a
tail = self.head
temp = a
while temp != None:
tail.next = temp.next
tail = temp.next
temp = temp.next
# Count the number of nodes in the list
def countNode(self):
count = 0
temp = self.head
while temp != None:
count += 1
temp = temp.next
return count
# Print elements in the list
def printList(self):
temp = self.head
while temp != None:
print(temp.element, end = ", " if (temp.next != None) else "" )
temp = temp.next
print()
# returns the reference of the Node at the given index. For invalid index return None.
def nodeAt(self, idx):
if (idx < 0 or idx >= self.countNode()):
print("Invalid Index")
else:
temp = self.head
i = 0
while temp != None:
if (i == idx):
return temp
i += 1
temp = temp.next
# returns the element of the Node at the given index. For invalid idx return None.
def get(self, idx):
if (idx < 0 or idx >= self.countNode()):
return None
else:
temp = self.head
i = 0
while temp != None:
if (i == idx):
return temp.element
i += 1
temp = temp.next
# updates the element of the Node at the given index.
# Returns the old element that was replaced. For invalid index return None.
# parameter: index, element
def set(self, idx, elem):
replaced_value = 0
if (idx < 0 or idx >= self.countNode()):
return None
else:
temp = self.head
i = 0
while temp != None:
if (i == idx):
replaced_value = temp.element
temp.element = elem
return replaced_value
i += 1
temp = temp.next
# returns the index of the Node containing the given element.
# if the element does not exist in the List, return -1.
def indexOf(self, elem):
flag = False
temp = self.head
i = 0
while temp != None:
if (temp.element == elem):
return_value = i
flag = True
break;
temp = temp.next
i += 1
return return_value if (flag) else -1
# returns true if the element exists in the List, return false otherwise.
def contains(self, elem):
flag = False
temp = self.head
i = 0
while temp != None:
if (temp.element == elem):
return_value = i
flag = True
break;
temp = temp.next
i += 1
return flag
# Makes a duplicate copy of the given List. Returns the reference of the duplicate list.
def copyList(self):
copy_list = Node(self.head.element, None)
tail = copy_list
temp = self.head
while temp != None:
tail.next = temp.next
tail = temp.next
temp = temp.next
return copy_list
# Makes a reversed copy of the given List. Returns the head reference of the reversed list.
def reverseList(self):
copy_list = Node((self.nodeAt(self.countNode()-1)).element, None)
tail = copy_list
for i in range(self.countNode()-2, -1, -1):
newNode = Node(self.nodeAt(i).element, None)
tail.next = newNode
tail = newNode
return copy_list
# inserts Node containing the given element at the given index
# Check validity of index.
def insert(self, elem, idx):
if idx <0 or idx > self.countNode():
print("Invalid Index")
elif idx == 0:
newNode= Node(elem, None)
newNode.next=self.head
self.head = newNode
return
else:
newNode = Node(elem, None)
predNode= self.nodeAt(idx-1)
newNode.next= predNode.next
predNode.next=newNode
return
# removes Node at the given index. returns element of the removed node.
# Check validity of index. return None if index is invalid.
def remove(self, idx):
rem_val = 0
if (idx < 0 or idx >= self.countNode()):
return None
else:
if idx == 0:
rem_node = self.head
self.head = self.head.next
rem_val = rem_node.element
rem_node.element = None
rem_node.next = None
else:
prev = self.nodeAt(idx-1)
rem_node = prev.next
prev.next = rem_node.next
rem_val = rem_node.element
rem_node.next = None
rem_node.element = None
return rem_val
# Rotates the list to the left by 1 position.
def rotateLeft(self):
temp = self.head
last_node = self.nodeAt(self.countNode()-1)
self.head = self.head.next
last_node.next = temp
temp.next = None
# Rotates the list to the right by 1 position.
def rotateRight(self):
last_prev_node = self.nodeAt(self.countNode()-2)
temp = self.head
self.head = last_prev_node.next
self.head.next = temp
last_prev_node.next = None