Reverse a linked listGiven pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing the links between nodes. Show
Examples:
Print reverse of a Linked List without actually reversingGiven a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1->2->3->4, then output should be 4->3->2->1. Recommended: Please try your approach on {IDE} first, before moving on to the solution. Algorithm printReverse(head) 1. call print reverse for head->next 2. print head->dataImplementation: C++
C
Java
Python3
C#
Javascript
Output: 4 3 2 1Time Complexity: O(n) Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Article Tags :
Linked List
Microsoft Practice Tags :
Microsoft Linked List Required knowledgeBasic C programming, Functions, Singly Linked List, Dynamic memory allocation Algorithm to reverse a Singly Linked ListAlgorithm to reverse a Singly Linked List
%%Input : head node of the linked list
Begin:
If (head != NULL) then
prevNode ← head
head ← head.next
curNode ← head
prevNode.next ← NULL
While (head != NULL) do
head ← head.next
curNode.next ← prevNode
prevNode ← curNode
curNode ← head
End while
head ← prevNode
End if
End Program to create a singly linked list of n nodes and display it in reverse orderExplanationIn this program, we need to create a singly linked list and display the list in reverse order. Original List Reversed List One of the approaches to solving this problem is to reach the end the of the list and display the nodes from tail to head recursively. Algorithm
SolutionPythonOutput: Original List:
1 2 3 4
Reversed List:
4 3 2 1
Output: Original List:
1 2 3 4
Reversed List:
4 3 2 1
JAVAOutput: Original List:
1 2 3 4
Reversed List:
4 3 2 1
C#Output: Original List:
1 2 3 4
Reversed List:
4 3 2 1
PHPOutput: Original List:
1 2 3 4
Reversed List:
4 3 2 1
Program to Reverse a Linked List in C++Posted in Programming LAST UPDATED: AUGUST 12, 2021 In this tutorial we will be solving the problem of reversing a Linked List by completely reversing the order of the data nodes in it. In simpler words the data node present at the end should become the starting point of the Linked List. Implementing Your Own Linked List on InterviewsSince using existing Java classes is now allowed on Programming Job interviews, you need to create your own to write code. For this example, I have created our own singly linked list class. Similar to java.util.LinkedListalso contains a nested static class Node, which represents a node in the linked list.This class contains an integer attribute to hold the data part and another Node reference to point to the next one in the list. If you want to create a Generic linked list, you should replace int with T, a generic type, as shown here. In order to demonstrate that our reverse method is working, we will not only have to create a linked list but also need to populate the linked list. In order to populate, you need to implement the add() method on the singly linked list. You have two choices, either add the element at the head or at the tail, adding an element to the head is easy as it doesn't require a traversal till the end but if you want to create a list that contains elements in the order they are added then we need to add nodes at the end of the linked list. I have also created a print() method to print all nodes of the singly linked list, separated by space. This method is very useful to demonstrate that our reverse method is actually working or not, as you can print the linked list before and after reversal. If you struggle with implementing essential data structures like a linked list, binary tree, the hash table in your own code on any programming language like Java then I suggest you joinAlgorithms and Data Structures - Part 1 and 2courses on Pluralsight. They will not only help you to write your data structure but also how to calculate time and space complexity. |