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
    • 100+ Java Programs
    • 100+ C Programs
  • 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

Reversing the Linked List

YASH PAL, 12 May 202028 May 2024

Reversing a linked list means changing the linked part of all nodes of a linked list to pointing to the nodes that come before every node.

Reversing the Linked list Data strctures

As you see we have a linked list that has four nodes and to reverse this list we need to point every node is linked part to the node that comes right before every node. and in the end, we update the start variable’s position to the last node of the list. as you see in the image given below.

Reversing the Linked list Data strctures

Method to reverse a linked list using links of nodes

To reverse a linked list using the links of nodes we need three references prev, p, and next. in the beginning, we set the prev variable to None and set the reference of the first node of the linked list to variable p.

Reversing the Linked list Data strctures

and then we start a loop till the value p does become None. means till we don’t reach the last node of the list. in every iteration, we set the reference of the node that comes after node p into the variable next.

Reversing the Linked list Data strctures

and then we set the linked part of node p equal to the prev variable. 

Reversing the Linked list Data strctures

after that, we store the reference of node p into variable prev. so now the variable prev points to the node that points to variable p.

Reversing the Linked list Data strctures

and then we change the reference of node p using the variable next. which means now the variable p points to the node that points to the variable next.

Reversing the Linked list Data strctures

now, these conditions will run until we will not reach the end of the list.

Reversing the Linked list Data strctures
Reversing the Linked list Data strctures
Reversing the Linked list Data strctures
Reversing the Linked list Data strctures
Reversing the Linked list Data strctures
Reversing the Linked list Data strctures
Reversing the Linked list Data strctures
Reversing the Linked list Data strctures
Reversing the Linked list Data strctures

Now we reached the end of the list because the linked part of node p is None. so now we update the self-variable value and point to the last node of the list.

Reversing the Linked list Data strctures

so now the linked list is reversed using the links of every node. 

here is the Python code to reverse a linked list using the links of every node.

def reverse_list(self):
    prev = None    p = self.start
    while p is not None:
        next = p.link
        p.link = prev
        prev = p
        p = next
    self.start = prev
Computer Science Tutorials Data Structures Tutorials computer scienceData Structure

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • 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
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes