-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedList.py
85 lines (66 loc) · 2.05 KB
/
linkedList.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
class Node:
def __init__(self,data):
self.data = data
self.next = None
class linked_list:
def __init__(self):
self.head = None
def instert_at_start(self, new_node):
if self.head == None:
self.head = new_node
new_node.next = self.head
self.head = new_node
def insert_at_end(self, new_node):
if self.head:
last_node = self.head
while last_node.next != None:
last_node = last_node.next
last_node.next = new_node
else:
self.head = new_node
def insert_at_custom(self,pos,new_node):
counter = 1
last_node = self.head
while last_node.next != None:
if counter == pos-1:
new_node.next = last_node.next
last_node.next = new_node
last_node = last_node.next
counter += 1
def delete_from_start(self):
node = self.head
self.head = self.head.next
del node
def delete_from_end(self):
if self.head:
last_node = self.head
prev_node = None
while last_node.next != None:
prev_node = last_node
last_node = last_node.next
prev_node.next = None
del last_node
def delete_from_custom(self, pos):
last_node = self.head
prev = None
counter = 1
while last_node.next != None:
if counter == pos:
node = last_node
prev.next = last_node.next
del node
prev = last_node
last_node = last_node.next
counter += 1
def printll(self):
temp_node = self.head
while temp_node != None:
print(temp_node.data)
temp_node = temp_node.next
lista = linked_list()
lista.insert_at_end(Node(6))
lista.insert_at_end(Node(7))
lista.instert_at_start(Node(8))
lista.delete_from_custom(2)
lista.insert_at_custom(2,Node(6))
lista.printll()