@@ -25,10 +25,10 @@ Let's code something
25
25
26
26
The smallest Unit: Node
27
27
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
32
32
33
33
Now, we will see the types of linked list.
34
34
@@ -45,9 +45,9 @@ Simply think it is a chain of nodes in which each node remember(contains) the ad
45
45
46
46
### Creating a linked list class
47
47
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
51
51
52
52
### Inserting a new node at the beginning of a linked list
53
53
@@ -130,37 +130,37 @@ Connect all the code.
130
130
### Deleting a node from the beginning of a linked list
131
131
check the list is empty otherwise shift the head to next node.
132
132
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
137
137
138
138
### Deleting a node from the end of a linked list
139
139
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
150
150
151
151
152
152
### Search in a linked list
153
153
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
+
164
164
165
165
166
166
0 commit comments