-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_linked_list.py
More file actions
68 lines (56 loc) · 1.51 KB
/
single_linked_list.py
File metadata and controls
68 lines (56 loc) · 1.51 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
class node:
def __init__(self,data=None):
self.data = data
self.next = None
class linked_list:
def __init__(self):
self.head = node()
def append(self, data):
new_node = node(data)
current = self.head
while(current.next != None):
current = current.next
current.next = new_node
def display(self):
values = []
current = self.head
while(current.next != None):
current = current.next
values.append(current.data)
print(values)
def length(self):
length = 0
current = self.head
while(current.next != None):
length += 1
current = current.next
return length
def get(self, loc):
if(loc > self.length()):
return "invalid location for a get"
current = self.head
while(loc != 0):
current = current.next
loc -= 1
print(current.data)
def delete(self, loc):
if(loc > self.length()):
return "invalid location for a deletion"
list_index = 0
current = self.head
while(True):
last_node = current
current = current.next
if(list_index == loc):
last_node.next = current.next
return
list_index += 1
ll = linked_list()
ll.append(5)
ll.append(4)
ll.append(3)
ll.append(2)
ll.append(6)
ll.display()
ll.delete(0) #index location
ll.display()