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 a doubly linked list in dsa

Reversing a Doubly Linked List

Posted on 20 May 202023 April 2023 By YASH PAL No Comments on Reversing a Doubly Linked List

To Reverse a doubly linked list we have two approaches either reverse the links of every node or either reverse the values of nodes. in this tutorial, we are going to reverse a doubly-linked list by reversing their links.

like if we have a doubly-linked list that has four nodes. as you see in the image given below.

reversing a doubly linked list in data structures

after reversing this linked list the start variable refers to the last node of the list and the first node becomes the last node and the last node becomes the first node. so our new reversed linked list should be like this as you see in the image given below.

reversing a doubly linked list in data structures

Method to Reversing a Doubly linked list

So to reverse a linked list first we need two references p1 and p2. initially, we set p1 to point to the first node of the linked list and p2 to point to the second node of the linked list.

reversing a doubly linked list in data structures

after that, we store the p1 node’s previous link part in the p1 node’s next link part. as you see in the given below image.

reversing a doubly linked list in data structures

and then we store the p2 node’s reference in the p1 node’s previous link part.

reversing a doubly linked list in data structures

after that, we start a loop that will be run till the value of variable p2 does become None or Null. it means the loop will run until we reached the last node of the linked list.

in every iteration first, we store the p2 node’s next link part’s value in the p2 node’s previous link part.

reversing a doubly linked list in data structures

second, we store the p1 node’s reference in p2 node’s next link part.

reversing a doubly linked list in data structures

third, we set p1 to the next node that comes after node p1 using the p2 node.

reversing a doubly linked list in data structures

fourth we set p2 to the next node that comes after node p2 using the previous link part of node p2.

reversing a doubly linked list in data structures

so in every iteration, these four conditions will apply and this condition will run till we don’t reach the last node of the linked list or till the value of the node, p2 becomes None or Null.

reversing a doubly linked list in data structures
reversing a doubly linked list in data structures
reversing a doubly linked list in data structures
reversing a doubly linked list in data structures
reversing a doubly linked list in data structures
reversing a doubly linked list in data structures
reversing a doubly linked list in data structures
reversing a doubly linked list in data structures

so now the value of variable p2 becomes None means we reached the last node of the linked list so we store the p1 node’s reference into the start variable because the start variable always refers to the first node of the linked list.

reversing a doubly linked list in data structures

Computer Science Tutorials, Data Structures Tutorials Tags:computer science, Data Structure

Post navigation

Previous Post: Deletion in Doubly Linked List
Next Post: Circular linked list in Data structure

Related Tutorials

python libraries for text transformation REPHRASING REVOLUTION: HOW PYTHON LIBRARIES FACILITATE TEXT TRANSFORMATION Computer Science Tutorials
Introduction to Statistics for Data Science: Building a Solid Foundation Computer Science Tutorials
programming languages for machine learning and data science Top Programming Languages For Machine Learning Computer Science Tutorials
How to Become a Successful Data Engineer in the Data Science Field – Complete Guide Computer Science Tutorials
Is Python a good language for Machine Learning/AI? Computer Science Tutorials
basics of boolean algebra Its Operators, Laws, and Examples Basics of Boolean Algebra: Its Operators, Laws, and Examples Boolean Algebra

Leave a Reply

You must be logged in to post a comment.

  • 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
  • Boolean Algebra
  • 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
  • Python Tutorials
  • 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