Skip to content

Commit 385268b

Browse files
committed
linked list implememntation
1 parent 31ec554 commit 385268b

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

Sprint-2/implement_linked_list/linked_list.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,27 @@ class LinkedList:
77
def __init__(self):
88
self.head = None
99
self.tail = None
10-
11-
def push_head(self, value):
12-
"""Add a value to the head. Return the node as a handle."""
10+
def push_head(self, value): #this is to add a value/node to the head/start.
1311
node = Node(value)
1412
node.next = self.head
15-
1613
if self.head:
1714
self.head.previous = node
1815
self.head = node
1916
if not self.tail:
20-
self.tail = node #list=empty,tail also points to new node
17+
self.tail = node #list=empty,tail also points to new node
2118
return node
22-
23-
def pop_tail(self):
19+
def pop_tail(self): #Remove the value from the tail of the list
2420
if not self.tail:
2521
return None
2622
value = self.tail.value
2723
prev_node = self.tail.previous
2824
if prev_node:
2925
prev_node.next = None
3026
self.tail = prev_node
31-
32-
3327
if not self.tail:
34-
self.head = None #If we removed the only element
35-
28+
self.head = None #If only one element is removed.
3629
return value
37-
38-
def remove(self, node):
30+
def remove(self, node): #remove any node
3931
if not node:
4032
return
4133
prev_node = node.previous
@@ -48,5 +40,5 @@ def remove(self, node):
4840
next_node.previous = prev_node
4941
else:
5042
self.tail = prev_node
51-
node.next = None # Disconnect node
43+
node.next = None #Disconnecting the node from the rest
5244
node.previous = None

0 commit comments

Comments
 (0)