Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Reverse a sublist of linked list

  • Difficulty Level : Hard
  • Last Updated : 05 May, 2022

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++ program to reverse a linked list
// from position m to position n
#include <bits/stdc++.h>
using namespace std;
 
// Linked list node
struct Node {
    int data;
    struct Node* next;
};
 
// function used to reverse a linked list
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;
}
 
// function used to reverse a linked list from position m to n
Node* reverseBetween(Node* head, int m, int n)
{
    if (m == n)
        return head;
 
    // revs and revend is start and end respectively of the
    // portion of the linked list which need to be reversed.
    // revs_prev is previous of starting position and
    // revend_next is next of end of list to be reversed.
    Node *revs = NULL, *revs_prev = NULL;
    Node *revend = NULL, *revend_next = NULL;
 
    // Find values of above pointers.
    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;
    // Reverse linked list starting with revs.
    revend = reverse(revs);
    // If starting position was not head
    if (revs_prev)
        revs_prev->next = revend;
    // If starting position was head
    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;
}
 
// function to add a new node at the
// beginning of the list
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;
}
 
// Driver code
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;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

C




// C program to reverse a linked list
// from position m to position n
#include <stdio.h>
#include <stdlib.h>
 
// Linked list node
typedef struct Node {
    int data;
    struct Node* next;
} Node;
 
// function used to reverse a linked list
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;
}
 
// function used to reverse a linked list from position m to n
Node* reverseBetween(Node* head, int m, int n)
{
    if (m == n)
        return head;
 
    // revs and revend is start and end respectively of the
    // portion of the linked list which need to be reversed.
    // revs_prev is previous of starting position and
    // revend_next is next of end of list to be reversed.
    Node *revs = NULL, *revs_prev = NULL;
    Node *revend = NULL, *revend_next = NULL;
 
    // Find values of above pointers.
    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;
    // Reverse linked list starting with revs.
    revend = reverse(revs);
    // If starting position was not head
    if (revs_prev)
        revs_prev->next = revend;
    // If starting position was head
    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");
}
 
// function to add a new node at the beginning of the list
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;
}
 
// Driver code
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;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Python3




# Python3 program to reverse a linked list
# from position m to position n
  
# Linked list node
class Node:
     
    def __init__(self, data):
         
        self.data = data
        self.next = None
 
# The standard reverse function used
# to reverse a linked list
def reverse(head):
 
    prev = None  
    curr = head
  
    while (curr):
        next = curr.next
        curr.next = prev
        prev = curr
        curr = next
     
    return prev
  
# Function used to reverse a linked list
# from position m to n which uses reverse
# function
def reverseBetween(head, m, n):
 
    if (m == n):
        return head
         
    # revs and revend is start and end respectively
    # of the portion of the linked list which
    # need to be reversed. revs_prev is previous
    # of starting position and revend_next is next
    # of end of list to be reversed.
    revs = None
    revs_prev = None
    revend = None
    revend_next = None
  
    # Find values of above pointers.
    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
  
    # Reverse linked list starting with
    # revs.
    revend = reverse(revs)
  
    # If starting position was not head
    if (revs_prev):
        revs_prev.next = revend
  
    # If starting position was head
    else:
        head = revend
  
    revs.next = revend_next
    return head
 
def prints(head):
 
    while (head != None):
        print(head.data, end = ' ')
        head = head.next
         
    print()
 
# Function to add a new node at the
# beginning of the list
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
 
# Driver code
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)
     
# This code is contributed by rutvik_56

Javascript




<script>
// JavaScript program to reverse a linked list
// from position m to position n
 
// Linked list node
class Node{
     
    constructor(data){
         
        this.data = data
        this.next = null
    }
}
 
// The standard reverse function used
// to reverse a linked list
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 used to reverse a linked list
// from position m to n which uses reverse
// function
function reverseBetween(head, m, n){
 
    if (m == n)
        return head
         
    // revs and revend is start and end respectively
    // of the portion of the linked list which
    // need to be reversed. revs_prev is previous
    // of starting position and revend_next is next
    // of end of list to be reversed.
    let revs = null
    let revs_prev = null
    let revend = null
    let revend_next = null
 
    // Find values of above pointers.
    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
 
    // Reverse linked list starting with
    // revs.
    revend = reverse(revs)
 
    // If starting position was not head
    if (revs_prev)
        revs_prev.next = revend
 
    // If starting position was head
    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 to add a new node at the
// beginning of the list
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
}
 
// Driver code
     
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)
 
// This code is contributed by shinjanpatra
 
</script>

Output:

10 20 60 50 40 30 70

This article is contributed by Akshit Agarwal. 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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!