🌑

Stephen Cheng

Delete and Insert a Node in a Linked List in Python

 

Stephen Cheng

Intro

Basic things we can do with linked lists are:

  • Traversal
  • Remove a node
  • Insert a node
  • Sort

For simplicity, singly linked lists will be used to explain these operations below.

Delete a Node in a Linked List

In this case we have the link (or pointer or address) to a node that we want to delete. It is important to connect the nodes on each side of the node before deleting it, so that the linked list is not broken. So before deleting the node, we need to get the next pointer from the previous node, and connect the previous node to the new next node before deleting the node in between.

In a singly linked list, like we have here, to get the next pointer from the previous node we actually need traverse the list from the start, because there is no way to go backwards from the node we want to delete.

In the code below, the algorithm to delete a node is moved into a function called deleteSpecificNode, the return value is the new head of the linked list. So for example, if the node to be deleted is the first node, the new head returned will be the next node.

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
class Node:
def __init__(self, data):
self.data = data
self.next = None

def traverseAndPrint(head):
currentNode = head
while currentNode:
print(currentNode.data, end=" -> ")
currentNode = currentNode.next
print("null")

def deleteSpecificNode(head, nodeToDelete):

if head == nodeToDelete:
return head.next

currentNode = head
while currentNode.next and currentNode.next != nodeToDelete:
currentNode = currentNode.next

if currentNode.next is None:
return head

currentNode.next = currentNode.next.next

return head

node1 = Node(7)
node2 = Node(11)
node3 = Node(3)
node4 = Node(2)
node5 = Node(9)

node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5

print("Before deletion:")
traverseAndPrint(node1)

# Delete node4
node1 = deleteSpecificNode(node1, node4)

print("\nAfter deletion:")
traverseAndPrint(node1)

Insert a Node in a Linked List

Inserting a node into a linked list is very similar to deleting a node, because in both cases we need to take care of the next pointers to make sure we do not break the linked list. To insert a node in a linked list we first need to create the node, and then at the position where we insert it, we need to adjust the pointers so that the previous node points to the new node, and the new node points to the correct next node.

In the insertNodeAtPosition function below, the return value is the new head of the linked list. So for example, if the node is inserted at the start of the linked list, the new head returned will be the new node.

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
class Node:
def __init__(self, data):
self.data = data
self.next = None

def traverseAndPrint(head):
currentNode = head
while currentNode:
print(currentNode.data, end=" -> ")
currentNode = currentNode.next
print("null")

def insertNodeAtPosition(head, newNode, position):
if position == 1:
newNode.next = head
return newNode

currentNode = head
for _ in range(position - 2):
if currentNode is None:
break
currentNode = currentNode.next

newNode.next = currentNode.next
currentNode.next = newNode
return head

node1 = Node(7)
node2 = Node(3)
node3 = Node(2)
node4 = Node(9)

node1.next = node2
node2.next = node3
node3.next = node4

print("Original list:")
traverseAndPrint(node1)

# Insert a new node with value 97 at position 2
newNode = Node(97)
node1 = insertNodeAtPosition(node1, newNode, 2)

print("\nAfter insertion:")
traverseAndPrint(node1)

, , — Jul 22, 2022

Search

    Made with ❤️ and ☀️ on Earth.