Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Operations on the linked list

YASH PAL, 12 May 202028 May 2024

To perform operations like insertion, deletion, sorting, and searching on the linked list we need to find the reference to a particular node in a linked list. so today we are going to learn how we can find the reference to the nodes to perform operations on a linked list.

After completing this tutorial you are able to learn

  • Reference to the last node
  • Reference to the second last node
  • Reference to a node with particular info
  • Reference to the predecessor of a node with particular info
  • Reference to a node at a particular position

Finding a reference to the last node

Operations - Linked list in Data Structures and algorithms

as we know linked list contains the null or None value at the linked part of the last node of the list. so here is the code to find the reference to the last node of the linked list.

p = self.start
while p.link is not None:
    p = p.link

Note: here we first store the reference of the first node of the list in the p variable and then we continue to the next using the while loop till the value of the link part of a node is not equal to None.

Finding a reference to the second last node

Operations - Linked list in Data Structures and algorithms

here is the Python code to find the reference to the second last node of the list.

p = self.start
while p.link.link is not None:
    p = p.link

Note: here we run the while loop till the value of next to the next link of the node is equal to the null of None.

Finding a reference to a node with particular info

Operations - Linked list in Data Structures and algorithms

let’s assume we need to find the node that contains the value x. For example, if we need to find the node that contains the value 30 then here is the Python code to find the node with a particular value.

p = self.start
while p is not None:
    if p.info == x:
        break    p = p.link

Note: here we run the while loop till the last node of the list. and if we found the value of the node that is equal to the value that we are searching then the break statement stops the while loop.

Finding a reference to the predecessor of a node with particular info

Predecessor: predecessor node is the node before the node that contains the particular value. as you see in the image given below.

Operations - Linked list in Data Structures and algorithms

Note: here we need to find the reference to the predecessor of the node that contains the value x=30.

Here is the Python code to find the reference to the predecessor of the node with particular info.

p = self.start
while p.link is not None:
    if p.link.info == x:
        break    p = p.link

here we run the loop till the last node of the linked list and at every node, we check if the info part of the next to next node is equal to the value x or not. if the value is equal to the x then we break the loop.

Finding a reference to a node at a particular position

Operations - Linked list in Data Structures and algorithms

Here we need to find the node at position 3 as you see in the image given above.

Here is the Python code to find the node at a particular position.

p = self.start
i = 1
while i<k and p is not None:
    p = p.link
    i += 1

Note: here we run the while loop till the value of i is less than k and the last node to the list. and we increment the value of I at every iteration.

Computer Science Tutorials Data Structures Tutorials computer scienceData Structure

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes