Skip to content
  • Linkedin
  • Youtube
  • Pinterest
  • Home
  • Privacy Policy
  • About
  • Contact
Programmingoneonone

Programmingoneonone

Programmingoneonone is a website that publishes daily tutorials, methods, guides, and articles on IT, Education, and technology.

  • Home
  • Human Values
  • DSA
  • IoT Tutorials
  • Interview Questions and Answers
  • Toggle search form
Reversing linked list in data structures and algorithms

Reversing the Linked List

Posted on 12 May 202022 April 2023 By YASH PAL No Comments on Reversing the Linked List

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 Tags:computer science, Data Structure

Post navigation

Previous Post: Deletion in Linked list | Data structures
Next Post: Sorting a Linked list in Data structures

Related Tutorials

vi editing commands VI Editing Commands For Linux Computer Science Tutorials
modes of vi editor VI Editor in Linux Computer Science Tutorials
environment and path setting in linux Environment and Path Setting in Linux Computer Science Tutorials
hard and synbolic links in linux Hard links and Symbolic links Computer Science Tutorials
changing file access permissions Changing File Access Permissions in Linux Computer Science Tutorials
access permissions in linux Access permissions in Linux Computer Science Tutorials

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick your Subject

  • Internet of Things
  • Data Structures/Algorithms
  • Interview Preparation
  • Human Values
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

  • Algorithms Tutorials
  • Basic Programming
  • C Programming Tutorials
  • C++ Tutorials
  • Compiler Design Tutorials
  • Computer Networks Tutorials
  • Computer Organization Tutorials
  • Computer Science Tutorials
  • Data Structures Tutorials
  • DBMS Tutorials
  • Developer Guide
  • Digital Communication
  • Digital Logic Tutorials
  • Internet of Things Tutorials
  • Internet Tutorials
  • Interview questions answers
  • Java Tutorials
  • Javascript Tutorials
  • Linux
  • Machine Learning Tutorials
  • Operating Systems Tutorials
  • Programming Tutorials
  • Projects
  • Tips&Tricks
  • Tools
  • VBScript Tutorials
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

Copyright © 2023 Programmingoneonone.

Powered by PressBook Blog WordPress theme