Program for Linked List Reverse in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

A linked list is an abstract data structure, consisting of a linear collection of nodes. Each node contains data as well as information for the next node in the sequence. Many algorithmic problems can be constructed over the linked list. One such problem is reversing a linked list in place. An iterative or recursive approach can be used to solve this problem by reversing the direction between each link.

Linked List in Python

A linked list is a data structure that contains a collection of nodes in a sequence. Each node contains data and a pointer to the next node in the sequence. There is a special pointer called the head pointer. This pointer is used to perform operations on the linked list like traversal, insertion, etc. In Python, a linked list is implemented using object-oriented programming. The class Node contains two members, a data field and a reference to the next node. Another class LinkedList is used that stores the reference to the head pointer of the linked list. This class can contain methods to perform CRUD(Create, Read, Update, Delete) operations on the linked list. Below is an implementation of the class Node and the class LinkedList in Python. For simplicity, the data field is taken as an integer. Implementation

Output

Reversing a Linked List in Python

Problem Statement

Given a head pointer of a linked list, the problem is to reverse the linked list in place. This means we are neither allowed to shuffle the data field nor use auxiliary space to create a new linked list. This problem is commonly known as linked list reverse in Python.

Example

Suppose the nodes of the linked list are numbered and links are represented using arrows. So if we are given the headhead pointer to the linked list: 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5 Then our algorithm should provide a linked list reverse in Python as: 5 \rightarrow 4 \rightarrow 3 \rightarrow 2 \rightarrow 1 The new head pointer should be at node 5. Reversing a Linked List

Algorithm

The basic idea of the problem is to traverse the linked list and reverse the links between nodes one at a time. For example, suppose we start with: 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5 First, we will reverse the link between node 1 and node 2. 1 \leftarrow 2 Next, we will reverse the link between node 2 and node 3. 1 \leftarrow 2 \leftarrow 3 We will repeat the process until we reverse all the links to get: 1 \leftarrow 2 \leftarrow 3 \leftarrow 4 \leftarrow 5

Reversing a Linked List in Python by an Iterative Approach

Algorithm

Let us consider a simple example to understand the algorithm to get linked list reverse in Python.

Consider two pointers prevprev pointing to the previous node of the headhead pointer and currcurr pointing to the headhead pointer. Initially, the prevprev pointer is None.

Reversing a Linked List in Python by an iterative approach

Step 1: Reverse the link represented by the pointers prevprev and currcurr. To traverse the rest of the list, we need a temporary variable nextnext that points to the next node in the list after the currcurr node. Reversing a Linked List in Python by an iterative approach 2 Step 2: Update the prevprev and currcurr pointer. prevprev pointer will become currcurr and currcurr pointer is updated to nextnext pointer. As we traverse the linked list, we build the linked list reverse in Python. Reversing a Linked List in Python by an iterative approach 3

We perform the above steps until the currcurr pointer is None.

Implementation

Output

Complexity Analysis

Time Complexity

To solve linked list reverse in Python, we iterate over the list once from start to end and make a constant number of pointer changes. So the time complexity is O(N)O(N), where NN is the number of nodes present in the linked list.

Time Complexity: O(N)O(N)

Space Complexity

In the implementation, we are using a constant number of variables, hence the space complexity is O(1)O(1).

Space Complexity: O(1)O(1)

Reversing a Linked List in Python by a Simpler and Tail Recursive Method

Algorithm

In this method, we reverse links in the linked list from the end. The key observation is that, if we remove the first node of the linked list, then we end up with a smaller subproblem. So, we divide the linked list into two parts:

  • The first node
  • Rest of the linked list

After recursion on the remaining linked list, the link connecting the first node with the linked list is reversed.

Consider the example below. We use a currcurr pointer for indicating the first node at each recursion. Reversing a Linked List in Python by a Simpler and Tail Recursive Method

We call the recursion on the linked list until we are at the last node. In this case, we make this node the new headhead node and return the currcurr node.

Reversing a Linked List in Python by a Simpler and Tail Recursive Method 2

Now when the function returns, we have a headhead pointer to the reversed list and the pointer to the currcurr node. So we add this currcurr node to the end of the reversed linked list. This is done by reversing the link between currcurr and curr.nextcurr.next.

In this way, we solve the problem linked list reverse in Python recursively.

Implementation

Output

Complexity Analysis

Time Complexity

We iterate over the linked list once using recursion. At each recursion call, we do a constant number of pointer changes, hence the time complexity is O(N)O(N), where NN is the number of nodes in the linked list.

Time Complexity: O(N)O(N)

Space Complexity

In the implementation, the number of recursion calls is equal to the number of nodes in the linked list. Each recursive call uses the program stack to store information, hence the space complexity is O(N)O(N), where NN is the number of nodes in the linked list.

Space Complexity: O(N)O(N)

Tail Recursive Implementation

Tail Recursion refers to a recursive function in which the function call is the last statement of the function. The function call is the last statement that is executed by the compiler. This is beneficial as compilers can reduce the running time of the program. Since the function call is the last statement to be executed, the compiler does not need to store the states of the current program in the operating system stack. It can just return the result of the function call directly. Multiple stack push and pop operations are not performed in a tail recursive function, so the overall running time and auxiliary space decrease.

The logic to solve linked list reverse in Python using the tail recursive method remains the same, however, a new tail recursive function is added. The function reverseLinkedListTailRecursive reverses the linked list by reversing the links one by one from the beginning until the last node is reached. When the last node is reached, it makes this node the new head of the list.

Complexity Analysis

Time Complexity

We iterate over the linked list once using recursion, hence the time complexity is O(N)O(N), where NN is the number of nodes in the linked list.

Time Complexity: O(N)O(N)

Space Complexity

In the implementation, the number of recursion calls is equal to the number of nodes in the linked list. Since we are using tail recursion, there is no need to store the states of the program in the operating system stack, so the space complexity is O(1)O(1).

Space Complexity: O(1)O(1)

Conclusion

  • A linked list reverse in python can be solved in place by iterating through the linked list and reversing each link between the nodes, one by one.
  • A simpler recursive approach can be used where the links are reversed from the end of the linked list.
  • The recursive approach is simpler to implement but requires O(N)O(N) space. The iterative approach requires O(1)O(1) space.
  • The time complexity to reverse the linked list is O(N)O(N) in both iterative and recursive methods.