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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
struct Node* reverse( struct Node* head)
{
struct Node* prev = NULL;
struct Node* curr = head;
while (curr) {
struct Node* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
Node* reverseBetween(Node* head, int m, int n)
{
if (m == n)
return head;
Node *revs = NULL, *revs_prev = NULL;
Node *revend = NULL, *revend_next = NULL;
int i = 1;
Node* curr = head;
while (curr && i <= n) {
if (i < m)
revs_prev = curr;
if (i == m)
revs = curr;
if (i == n) {
revend = curr;
revend_next = curr->next;
}
curr = curr->next;
i++;
}
revend->next = NULL;
revend = reverse(revs);
if (revs_prev)
revs_prev->next = revend;
else
head = revend;
revs->next = revend_next;
return head;
}
void print( struct Node* head)
{
while (head != NULL) {
cout<<head->data<< " " ;
head = head->next;
}
cout<<endl;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct Node* head = NULL;
push(&head, 70);
push(&head, 60);
push(&head, 50);
push(&head, 40);
push(&head, 30);
push(&head, 20);
push(&head, 10);
reverseBetween(head, 3, 6);
print(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* reverse(Node* head)
{
Node* prev = NULL;
Node* curr = head;
while (curr) {
Node* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
Node* reverseBetween(Node* head, int m, int n)
{
if (m == n)
return head;
Node *revs = NULL, *revs_prev = NULL;
Node *revend = NULL, *revend_next = NULL;
int i = 1;
Node* curr = head;
while (curr && i <= n) {
if (i < m)
revs_prev = curr;
if (i == m)
revs = curr;
if (i == n) {
revend = curr;
revend_next = curr->next;
}
curr = curr->next;
i++;
}
revend->next = NULL;
revend = reverse(revs);
if (revs_prev)
revs_prev->next = revend;
else
head = revend;
revs->next = revend_next;
return head;
}
void print(Node* head)
{
while (head != NULL) {
printf ( "%d " , head->data);
head = head->next;
}
printf ( "\n" );
}
void push(Node** head_ref, int new_data)
{
Node* new_node = (Node*) malloc ( sizeof (Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
Node* head = NULL;
push(&head, 70);
push(&head, 60);
push(&head, 50);
push(&head, 40);
push(&head, 30);
push(&head, 20);
push(&head, 10);
reverseBetween(head, 3, 6);
print(head);
return 0;
}
|
Java
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
static Node reverse(Node node)
{
Node prev = null ;
Node current = node;
while (current != null ) {
Node next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
}
static Node reverseBetween(Node head, int m, int n)
{
if (m == n)
return head;
Node revs = null , revs_prev = null ;
Node revend = null , revend_next = null ;
int i = 1 ;
Node curr = head;
while (curr!= null && i <= n) {
if (i < m)
revs_prev = curr;
if (i == m)
revs = curr;
if (i == n) {
revend = curr;
revend_next = curr.next;
}
curr = curr.next;
i++;
}
revend.next = null ;
revend = reverse(revs);
if (revs_prev!= null )
revs_prev.next = revend;
else
head = revend;
revs.next = revend_next;
return head;
}
void printList(Node node)
{
while (node != null ) {
System.out.print(node.data + " " );
node = node.next;
}
}
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node( 10 );
list.head.next = new Node( 20 );
list.head.next.next = new Node( 30 );
list.head.next.next.next = new Node( 40 );
list.head.next.next.next.next = new Node( 50 );
list.head.next.next.next.next.next = new Node( 60 );
list.head.next.next.next.next.next.next = new Node( 70 );
reverseBetween(head, 3 , 6 );
list.printList(head);
}
}
|
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):
if (m = = n):
return head
revs = None
revs_prev = None
revend = None
revend_next = None
i = 1
curr = head
while (curr and i < = n):
if (i < m):
revs_prev = curr
if (i = = m):
revs = curr
if (i = = n):
revend = curr
revend_next = curr. next
curr = curr. next
i + = 1
revend. next = None
revend = reverse(revs)
if (revs_prev):
revs_prev. next = revend
else :
head = revend
revs. next = revend_next
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;
}
}
static public Node reverse(Node head)
{
Node prev = null , current = head, next = null ;
while (current != null ) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
public void reverseBetween( int m, int n)
{
if (m == n)
return ;
Node revs = null , revs_prev = null ;
Node revend = null , revend_next = null ;
int i = 1;
Node curr = head;
while (curr!= null && i <= n) {
if (i < m)
revs_prev = curr;
if (i == m)
revs = curr;
if (i == n) {
revend = curr;
revend_next = curr.next;
}
curr = curr.next;
i++;
}
revend.next = null ;
revend = reverse(revs);
if (revs_prev!= null )
revs_prev.next = revend;
else
head = revend;
revs.next = revend_next;
}
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 reverse(head){
let prev = null
let curr = head
while (curr){
let next = curr.next
curr.next = prev
prev = curr
curr = next
}
return prev
}
function reverseBetween(head, m, n){
if (m == n)
return head
let revs = null
let revs_prev = null
let revend = null
let revend_next = null
let i = 1
let curr = head
while (curr && i <= n){
if (i < m)
revs_prev = curr
if (i == m)
revs = curr
if (i == n){
revend = curr
revend_next = curr.next
}
curr = curr.next
i += 1
}
revend.next = null
revend = reverse(revs)
if (revs_prev)
revs_prev.next = revend
else
head = revend
revs.next = revend_next
return head
}
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 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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
Node* reverseBetween(Node* head, 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;
return head;
}
void print( struct Node* head)
{
while (head != NULL) {
cout << head->data << " " ;
head = head->next;
}
cout << endl;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct Node* head = NULL;
push(&head, 70);
push(&head, 60);
push(&head, 50);
push(&head, 40);
push(&head, 30);
push(&head, 20);
push(&head, 10);
struct Node* nhead = reverseBetween(head, 3, 6);
print(nhead);
return 0;
}
|
Java
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
static Node reverseBetween(Node head, 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;
return head;
}
void printList(Node node)
{
while (node != null ) {
System.out.print(node.data + " " );
node = node.next;
}
}
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node( 10 );
list.head.next = new Node( 20 );
list.head.next.next = new Node( 30 );
list.head.next.next.next = new Node( 40 );
list.head.next.next.next.next = new Node( 50 );
list.head.next.next.next.next.next = new Node( 60 );
list.head.next.next.next.next.next.next
= new Node( 70 );
reverseBetween(head, 3 , 6 );
list.printList(head);
}
}
|
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.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
10 Jan, 2023
Like Article
Save Article