Skip to content

Commit 4a05766

Browse files
Update Linked-list.md
change2
1 parent d2a25f1 commit 4a05766

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

contrib/ds-algorithms/Linked-list.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ Let's code something
2525

2626
The smallest Unit: Node
2727

28-
class Node:
29-
def __init__(self, data):
30-
self.data = data # Assigns the given data to the node
31-
self.next = None # Initialize the next attribute to null
28+
class Node:
29+
def __init__(self, data):
30+
self.data = data # Assigns the given data to the node
31+
self.next = None # Initialize the next attribute to null
3232

3333
Now, we will see the types of linked list.
3434

@@ -45,9 +45,9 @@ Simply think it is a chain of nodes in which each node remember(contains) the ad
4545

4646
### Creating a linked list class
4747

48-
class LinkedList:
49-
def __init__(self):
50-
self.head = None # Initialize head as None
48+
class LinkedList:
49+
def __init__(self):
50+
self.head = None # Initialize head as None
5151

5252
### Inserting a new node at the beginning of a linked list
5353

@@ -130,37 +130,37 @@ Connect all the code.
130130
### Deleting a node from the beginning of a linked list
131131
check the list is empty otherwise shift the head to next node.
132132

133-
def deleteFromBeginning(self):
134-
if self.head is None:
135-
return "The list is empty" # If the list is empty, return this string
136-
self.head = self.head.next # Otherwise, remove the head by making the next node the new head
133+
def deleteFromBeginning(self):
134+
if self.head is None:
135+
return "The list is empty" # If the list is empty, return this string
136+
self.head = self.head.next # Otherwise, remove the head by making the next node the new head
137137

138138
### Deleting a node from the end of a linked list
139139

140-
def deleteFromEnd(self):
141-
if self.head is None:
142-
return "The list is empty"
143-
if self.head.next is None:
144-
self.head = None # If there's only one node, remove the head by making it None
145-
return
146-
temp = self.head
147-
while temp.next.next: # Otherwise, go to the second-last node
148-
temp = temp.next
149-
temp.next = None # Remove the last node by setting the next pointer of the second-last node to None
140+
def deleteFromEnd(self):
141+
if self.head is None:
142+
return "The list is empty"
143+
if self.head.next is None:
144+
self.head = None # If there's only one node, remove the head by making it None
145+
return
146+
temp = self.head
147+
while temp.next.next: # Otherwise, go to the second-last node
148+
temp = temp.next
149+
temp.next = None # Remove the last node by setting the next pointer of the second-last node to None
150150

151151

152152
### Search in a linked list
153153

154-
def search(self, value):
155-
current = self.head # Start with the head of the list
156-
position = 0 # Counter to keep track of the position
157-
while current: # Traverse the list
158-
if current.data == value: # Compare the list's data to the search value
159-
return f"Value '{value}' found at position {position}" # Print the value if a match is found
160-
current = current.next
161-
position += 1
162-
return f"Value '{value}' not found in the list"
163-
154+
def search(self, value):
155+
current = self.head # Start with the head of the list
156+
position = 0 # Counter to keep track of the position
157+
while current: # Traverse the list
158+
if current.data == value: # Compare the list's data to the search value
159+
return f"Value '{value}' found at position {position}" # Print the value if a match is found
160+
current = current.next
161+
position += 1
162+
return f"Value '{value}' not found in the list"
163+
164164

165165

166166

0 commit comments

Comments
 (0)