How to Create Reverse a Linked List in Python

In this tutorial, we will program "How to Reverse a Linked List in Python". We will learn how to identify and reverse a linked list. The objective is to safely and accurately reverse the given linked list. I will provide a sample program to demonstrate the coding process.

This topic is very easy to understand. Just follow the instructions I provide, and you'll be able to do it yourself with ease. The program I’ll show you demonstrates the proper way to reverse a linked list. I’ll also provide a simple and efficient method to achieve this. So, let's start coding!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. class Node:
  2.  
  3.         def __init__(self, data):
  4.                 self.data = data
  5.                 self.next = None
  6.  
  7.  
  8. class LinkedList:
  9.  
  10.  
  11.         def __init__(self):
  12.                 self.head = None
  13.  
  14.         def reverseUtil(self, curr, prev):
  15.  
  16.        
  17.                 if curr.next is None:
  18.                         self.head = curr
  19.  
  20.                        
  21.                         curr.next = prev
  22.                         return
  23.  
  24.                
  25.                 next = curr.next
  26.  
  27.                
  28.                 curr.next = prev
  29.  
  30.                 self.reverseUtil(next, curr)
  31.  
  32.  
  33.         def reverse(self):
  34.                 if self.head is None:
  35.                         return
  36.                 self.reverseUtil(self.head, None)
  37.  
  38.        
  39.  
  40.         def push(self, new_data):
  41.                 new_node = Node(new_data)
  42.                 new_node.next = self.head
  43.                 self.head = new_node
  44.  
  45.        
  46.         def printList(self):
  47.                 temp = self.head
  48.                 while(temp):
  49.                         print(temp.data,end=" ")
  50.                         temp = temp.next
  51.  
  52.  
  53.  
  54. llist = LinkedList()
  55. llist.push(8)
  56. llist.push(7)
  57. llist.push(6)
  58. llist.push(5)
  59. llist.push(4)
  60. llist.push(3)
  61. llist.push(2)
  62. llist.push(1)
  63.  
  64. print("\n==================== Reverse a Linked List====================")
  65.  
  66. print("\nMy list")
  67. llist.printList()
  68.  
  69. llist.reverse()
  70.  
  71. print("\n\nReverse linked list")
  72. llist.printList()

This script generates a star pattern resembling a Christmas tree. It uses nested loops to print different sections of the tree, including the triangular parts and the trunk. The program first displays a small triangular section, followed by progressively larger sections, then concludes with a trunk made of stars.

The script keeps running in a loop, allowing the user to repeat or exit the program based on their input (yes or no). Each loop iteration redraws the star pattern.

Output:

There you have it we successfully created How to Create Reverse a Linked List in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment