forked from oviiii-m/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingly_linked_list.py
More file actions
104 lines (74 loc) · 2.08 KB
/
singly_linked_list.py
File metadata and controls
104 lines (74 loc) · 2.08 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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def print_list(self):
temp = self.head
linked_list = ''
while temp:
linked_list += str(temp.data) + " -> "
temp = temp.next
print(linked_list)
# lists start at 0
def insert_node(self, val, pos):
target = Node(val)
# specific case for replacing head
if pos == 0:
target.next = self.head
self.head = target
return
def get_prev(position):
temp = self.head
count = 1
while count < position:
temp = temp.next
count += 1
return temp
# Getting previous node
prev = get_prev(pos)
if prev.next:
# Temp variable for upcoming node
next_node = prev.next
# Set previous next to our target node
prev.next = target
# Set next node of target node from temp variable
target.next = next_node
def delete_node(self, key):
temp = self.head
if temp is None:
return
if temp.data == key:
self.head = temp.next
temp = None
return
while temp.next.data != key:
temp = temp.next
# Getting target node
target_node = temp.next
# Set previous node's next to target's next
temp.next = target_node.next
# Remove target node's pointer
target_node.next = None
# Nodes: 4 -> 5 -> 7 -> 2
link = LinkedList()
link.head = Node(4)
first_node = Node(5)
second_node = Node(7)
third_node = Node(2)
link.head.next = first_node
first_node.next = second_node
second_node.next = third_node
link.print_list()
# Nodes: 4 -> 5 -> 7 -> 2
# Insert 3 at index 2
# Nodes: 4 -> 5 -> 3 -> 7 -> 2
link.insert_node(3, 2)
link.print_list()
# Nodes: 4 -> 5 -> 3 -> 7 -> 2
# Delete 3
# Nodes: 4 -> 5 -> 7 -> 2
link.delete_node(3)
link.print_list()