Linked Lists — Part 2: Mastering Insertion and Manipulation
Simplicity is the key to brilliance
Insertion to linked list in Memory :
1) At the Beginning of the linked list
2) After a node, in the middle of linked list : At the Specific position
3) At end of the linked list
Insert an Element at the beginning of Singly Linked list — Prepend method
1) Create new node to insert at the beginning of Linked List
2) Update the new Node’s next Pointer to point to the first node new_node.next=self.head
3) Update Head, to point new node : self.head=new_node
class Node:
def __init__(self,value):
self.value=value
self.next=None
class Linkedlist:
def __init__(self):
self.head=None
self.tail=None
self.length=0
def append(self,value):
new_node=Node(value)
if self.head==None:
self.head=new_node
self.tail=new_node
else:
self.tail.next=new_node
self.tail=new_node
def prepend(self,value):
new_node=Node(value)
if self.head is None:
self.head=new_node
self.tail=new_node
else:
new_node.next=self.head
self.head=new_node
self.length +=1
def __str__(self):
temp_node=self.head
result=""
while temp_node is not None:
result += str(temp_node.value)
if temp_node.next is not None:
result +='-->'
temp_node=temp_node.next
return result
newll=Linkedlist()
newll.append(10)
newll.append(20)
newll.append(30)
newll.append(40)
newll.prepend(50)
print(newll)
# output = 50-->10-->20-->30-->40
Insert an Element at the End of Singly Linked list — Append method
1) Create new node
2) Update the last Node’s next Pointer to point to the new node self.tail.next=new_node
3) Update tail, to point new node : self.tail=new_node
class Node:
def __init__(self,value):
self.value=value
self.next=None
class Linkedlist:
def __init__(self):
self.head=None
self.tail=None
self.length=0
def append(self,value):
new_node=Node(value)
if self.head is None:
self.head=new_node
self.tail=new_node
else:
self.tail.next=new_node
self.tail=new_node
self.length +=1
def __str__(self):
temp_node=self.head
result=""
while temp_node is not None:
result +=str(temp_node.value)
if temp_node.next is not None:
result +='-->'
temp_node=temp_node.next
return result
newll=Linkedlist()
newll.append(10)
newll.append(20)
newll.append(30)
newll.append(40)
newll.append(50)
print(newll)
# output 10-->20-->30-->40-->50
Inserting an element at the specific location of Singly Linked List
Programming is breaking of one big impossible task into several very small possible tasks.— Jazzwant
class Node:
def __init__(self, value):
self.value = value
self.next = None
class Linkedlist:
def __init__(self):
self.head = None
self.tail = None
self.length = 0
def append(self, value):
new_node = Node(value)
if self.head is None:
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
self.length += 1
def prepend(self, value):
new_node = Node(value)
if self.head is None:
self.head = new_node
self.tail = new_node
else:
new_node.next = self.head
self.head = new_node
self.length += 1
# value: The data to be inserted.
#position: The position where the new node should be inserted (0-based indexing).
def insert(self, value, position):
if position < 0:
raise IndexError("Invalid position")
# Create the new node
new_node = Node(value)
# If the list is empty or the position is 0, insert at the beginning
if self.head is None or position == 0:
self.prepend(value)
return
# Iterate to the node at the position-1
current = self.head
for i in range(position - 1):
if current.next is None:
raise IndexError("Invalid position")
current = current.next
# Insert the new node after the current node
new_node.next = current.next
current.next = new_node
self.length += 1
def __str__(self):
temp_node = self.head
result = ""
while temp_node is not None:
result += str(temp_node.value)
if temp_node.next is not None:
result += '-->'
temp_node = temp_node.next
return result
newll = Linkedlist()
newll.append(10)
newll.append(20)
newll.append(30)
newll.append(40)
# Insert 50 at position 2
newll.insert(50, 2)
print(newll)
# output = 10-->20-->50-->30-->40
Key Message : Coding used to be scary! So much code, like a giant puzzle! But then I learned in small bits, like little steps, and with words I knew. Now coding feels easy, like following a recipe! I can break down any problem now, one step at a time. All the best for new learners.
Thank you. If it is helpful, please follow me. Waiting for my first 100 followers !