-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1linkedlist.py
More file actions
130 lines (92 loc) · 2.48 KB
/
1linkedlist.py
File metadata and controls
130 lines (92 loc) · 2.48 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
class Node():
def __init__(self, data: int):
self.item = data
self.next = None
class LinkedList():
size = 0
def __init__(self, node: Node):
self.head = node
self.size += 1
def list_append(self, node: Node):
current = self.head
while current.next is not None:
current = current.next
current.next = node
self.size += 1
def add_to_front(self, node: Node):
node.next = self.head
self.head = node
self.size += 1
def delete_tail(self):
if self.head.next == None:
self.head = None
current = self.head
while current.next.next is not None:
current = current.next
# for i in range(1, self.size-1):
# current = current.next
current.next = None
self.size -= 1
def delete_head(self):
self.head = self.head.next
self.size -= 1
def delete(self, idx):
if idx > self.size or idx < 0:
raise ValueError
if idx == 1:
self.delete_head()
return
if idx == self.size:
self.delete_tail()
return
counter = 1
current = self.head
while counter < idx - 1:
current = current.next
counter += 1
current.next = current.next.next
self.size -= 1
def add(self, node: Node, idx: int):
if idx == 1:
self.add_to_front(node)
return
if idx == self.size:
self.list_append(node)
return
if idx > self.size or idx < 0:
raise ValueError
counter = 1
current = self.head
while counter < idx - 1:
current = current.next
counter += 1
node.next = current.next
current.next = node
self.size += 1
def show(self):
current = self.head
while current is not None:
print(current.item)
current = current.next
print()
print('-------')
# n1 = Node(10)
# n2 = Node(20)
# n3 = Node(30)
# head = Node(10)
# head.next = Node(20)
# n2.next = n3
# current = head
# while current is not None:
# print(current.item)
# current = current.next
myll = LinkedList(Node(55))
myll.list_append(Node(66))
myll.list_append(Node(78))
myll.list_append(Node(68))
myll.list_append(Node(88))
myll.show()
print(myll.size)
print('**************')
myll.add(Node(58), 2)
myll.show()