Reverse a sublist of linked list
We are given a linked list and positions m and n. We need to reverse the linked list from position m to n.
Examples:
Input : 10->20->30->40->50->60->70->NULL
m = 3, n = 6
Output : 10->20->60->50->40->30->70->NULL
Input : 1->2->3->4->5->6->NULL
m = 2, n = 4
Output : 1->4->3->2->5->6->NULL
To reverse the linked list from position m to n, we find addresses of start and end position of the linked list by running a loop, and then we unlink this part from the rest of the list and then use the normal linked list reverse function which we have earlier used for reversing the complete linked list, and use it to reverse the portion of the linked list which need to be reversed. After reversal, we again attach the portion reversed to the main list.
C++
C
< div id= "highlighter_887091" class = "syntaxhighlighter nogutter " ><table border= "0" cellpadding= "0" cellspacing= "0" ><tbody><tr><td class = "code" >< div class = "container" >< div class = "line number1 index0 alt2" ><code class = "comments" >
|
Java
Python3
C#
Javascript
Output
10 20 60 50 40 30 70
Time Complexity: O(N), Here N is the number of nodes in the linked list. In the worst case we need to traverse the list twice.
Auxiliary Space: O(1), As constant extra space is used.
Method 2: (single traversal)
In this method we need to traverse the list only once in the worst case. The algorithm makes used of the idea to reverse a normal linked list. Below is the algorithm:
- Get the pointer to the head and tail of the reversed linked list.
- Get the pointer to the node before mth and node after nth node.
- Reverse the list as discussed in this post.
- Connect back the links properly.
Implementation of the above approach:
C++
Java
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def reverse(head):
prev = None
curr = head
while (curr):
next = curr. next
curr. next = prev
prev = curr
curr = next
return prev
def reverseBetween(head, m, n):
curr = head
prev = None
i = 1
while i<m:
prev = curr
curr = curr. next
i + = 1
rtail = curr
rhead = None
while (i < = n):
temp = curr. next
curr. next = rhead
rhead = curr
curr = temp
i + = 1
if prev:
prev. next = rhead
else :
head = rhead
rtail. next = curr
return head
def prints(head):
while (head ! = None ):
print (head.data, end = ' ' )
head = head. next
print ()
def push(head_ref, new_data):
new_node = Node(new_data)
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
if __name__ = = '__main__' :
head = None
head = push(head, 70 )
head = push(head, 60 )
head = push(head, 50 )
head = push(head, 40 )
head = push(head, 30 )
head = push(head, 20 )
head = push(head, 10 )
reverseBetween(head, 3 , 6 )
prints(head)
|
C#
using System;
class LinkedList {
static Node head;
public class Node {
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
public void AddNode(Node node)
{
if (head == null )
head = node;
else {
Node temp = head;
while (temp.next != null ) {
temp = temp.next;
}
temp.next = node;
}
}
public void reverseBetween( int m, int n)
{
Node curr = head, prev = null ;
int i;
for (i = 1; i < m; i++) {
prev = curr;
curr = curr.next;
}
Node rtail = curr;
Node rhead = null ;
while (i <= n) {
Node next = curr.next;
curr.next = rhead;
rhead = curr;
curr = next;
i++;
}
if (prev != null )
prev.next = rhead;
else
head = rhead;
rtail.next = curr;
}
public void PrintList()
{
Node current = head;
while (current != null ) {
Console.Write(current.data + " " );
current = current.next;
}
Console.WriteLine();
}
}
class GFG{
public static void Main( string [] args)
{
LinkedList list = new LinkedList();
list.AddNode( new LinkedList.Node(10));
list.AddNode( new LinkedList.Node(20));
list.AddNode( new LinkedList.Node(30));
list.AddNode( new LinkedList.Node(40));
list.AddNode( new LinkedList.Node(50));
list.AddNode( new LinkedList.Node(60));
list.AddNode( new LinkedList.Node(70));
list.reverseBetween(3,6);
list.PrintList();
}
}
|
Javascript
<script>
class Node{
constructor(data){
this .data = data
this .next = null
}
}
function reverseBetween(head, m, n){
let curr = head;
let prev = null ;
let i;
for (i = 1; i < m; i++) {
prev = curr;
curr = curr.next;
}
let rtail = curr;
let rhead = null ;
while (i <= n) {
Node next = curr.next;
curr.next = rhead;
rhead = curr;
curr = next;
i++;
}
if (prev != null )
prev.next = rhead;
else
head = rhead;
rtail.next = curr;
}
function prints(head){
while (head != null ){
document.write(head.data, ' ' )
head = head.next
}
document.write( "</br>" )
}
function push(head_ref, new_data){
let new_node = new Node(new_data)
new_node.data = new_data
new_node.next = head_ref
head_ref = new_node
return head_ref
}
let head = null
head = push(head, 70)
head = push(head, 60)
head = push(head, 50)
head = push(head, 40)
head = push(head, 30)
head = push(head, 20)
head = push(head, 10)
reverseBetween(head, 3, 6)
prints(head)
</script>
|
Output
10 20 60 50 40 30 70
Time Complexity: O(n), Here n is the position n till which we have to reverse the linked list. In the worst case we need to traverse the list once when n is equal to the size of the linked list and in best case time complexity can go upto O(1).
Auxiliary Space: O(1), As constant extra space is used.
Last Updated :
10 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...