The Wayback Machine - https://web.archive.org/web/20240917134713/https://www.geeksforgeeks.org/rearrange-a-given-linked-list-in-place/
Open In App

Rearrange a given linked list in-place

Last Updated : 04 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Hard
47.9%
59.5K

Given a singly linked list L0 -> L1 -> â€Ķ -> Ln-1 -> Ln. Rearrange the nodes in the list so that the newly formed list is : L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 … You are required to do this in place without altering the nodes’ values. 

Examples: 

Input: 1 -> 2 -> 3 -> 4
Output: 1 -> 4 -> 2 -> 3
Explanation: Here n = 4, so the correct order is L0->L3->L1->L2

Input: 1 -> 2 -> 3 -> 4 -> 5
Output: 1 -> 5 -> 2 -> 4 -> 3
Explanation: Here n = 4, so the correct order is L0->L4->L1->L3->L2

[Naive Approach] Using two nested loops- O(n^2) Time and O(1) Space

The idea is to start traversing from head node. For each node, perform the following operations:

  • Traverse the list to find the last node.
  • Disconnect the last node from the list.
  • Place the removed last node after the current node.
  • Update current to its next node.

[Efficient Approach] By Reversing Second Half – O(n) Time and O(1) Space

  • Find the middle of the linked list using the fast and slow pointer method. This involves moving one pointer twice as fast as the other so that when the faster pointer reaches the end, the slower pointer will be at the middle.
  • Reverse the second half of the list starting just after the middle node and split them in two parts.
  • Merge the two halves together by alternating nodes from the first half with nodes from the reversed second half.

Below is the implementation of the above approach :

C++
// C++ code to rearrange a given linked list in-place
#include <bits/stdc++.h>
using namespace std;

class Node {
  public:
    int data;
    Node *next;

    Node(int x) {
        data = x;
        next = NULL;
    }
};

Node *reverselist(Node *head) {
    Node *prev = nullptr, *curr = head, *next;

    while (curr) {
        next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
}

void printList(Node *curr) {
  
    while (curr != nullptr) {
        cout << curr->data << " ";
        curr = curr->next;
    }
}

// Function to rearrange a linked list
Node *rearrange(Node *head) {
  
    // Find the middle point using tortoise and hare method
    Node *slow = head, *fast = slow->next;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }
  
    // Split the linked list into two halves
    Node *head1 = head;
    Node *head2 = slow->next;
    slow->next = NULL;
    
      // Reverse the second half
    head2 = reverselist(head2);
    
      // Merge alternate nodes
    head = new Node(0); 
    Node *curr = head;
    while (head1 || head2) {
      
        // First add the element from the first list
        if (head1) {
            curr->next = head1;
            curr = curr->next;
            head1 = head1->next;
        }
      
        // Then add the element from the second list
        if (head2) {
            curr->next = head2;
            curr = curr->next;
            head2 = head2->next;
        }
    }
  
    // Return the head of the new list
    return head->next;
}


int main() {
  
     // singly linked list 1->2->3->4->5
    Node *head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = new Node(4);
    head->next->next->next->next = new Node(5); 
      
    head = rearrange(head); 
    printList(head);        
    return 0;
}
C
// C code to rearrange a given linked list in-place

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};


struct Node *reverselist(struct Node *head) {
  
    // Initialize prev and current pointers
    struct Node *prev = NULL, *curr = head, *next;
    while (curr) {
        next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
}

void printList(struct Node *curr) {
    while (curr != NULL) {
        printf("%d ", curr->data);
        curr = curr->next;
    }
}

// Function to rearrange a linked list
struct Node *rearrange(struct Node *head) {
  
    // Find the middle point using tortoise 
      // and hare method
    struct Node *slow = head, *fast = slow->next;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }
    
    // Split the linked list into two halves
    // head1, head of first half
    // head2, head of second half
    struct Node *head1 = head;
    struct Node *head2 = slow->next;
    slow->next = NULL;
    
    // Reverse the second half
    head2 = reverselist(head2);
    
    // Merge alternate nodes
    struct Node dummy; 
    struct Node *curr = &dummy;
    dummy.next = NULL;
    
    while (head1 || head2) {
      
        // First add the element from head1
        if (head1) {
            curr->next = head1;
            curr = curr->next;
            head1 = head1->next;
        }
      
        // Then add the element from head2
        if (head2) {
            curr->next = head2;
            curr = curr->next;
            head2 = head2->next;
        }
    }
  
    return dummy.next;
}


struct Node *newNode(int key) {
    struct Node *temp = 
      (struct Node *)malloc(sizeof(struct Node));
    temp->data = key;
    temp->next = NULL;
    return temp;
}

int main() {
  
     // singly linked list 1->2->3->4->5
    struct Node *head = newNode(1);
    head->next = newNode(2);
    head->next->next = newNode(3);
    head->next->next->next = newNode(4);
    head->next->next->next->next = newNode(5);

    head = rearrange(head);
  
    printList(head);        
    return 0;
}
Java
// Java code to rearrange a given linked list in-place
class Node {
    int data;
    Node next;

    Node(int d) {
        data = d;
        next = null;
    }
}

class GfG {

     static void printList(Node node) {
        if (node == null) {
            return;
        }
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
    }

     static Node reverseList(Node node) {
        Node prev = null, curr = node, next;
        while (curr != null) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }

    // Method to rearrange the linked list
    static Node rearrange(Node node) {
      
        // Check if the list is empty or has only one node
        if (node == null || node.next == null) {
            return node;
        }

        // Find the middle point using tortoise and hare
        // method
        Node slow = node, fast = node.next;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        // Split the linked list into two halves
        Node firstHalf = node;
        Node secondHalf = slow.next;
        slow.next = null;

        // Reverse the second half of the list
        secondHalf = reverseList(secondHalf);

        // Merge alternate nodes from the two halves
        Node dummy = new Node(0);
        Node curr = dummy;
        while (firstHalf != null || secondHalf != null) {
            if (firstHalf != null) {
                curr.next = firstHalf;
                curr = curr.next;
                firstHalf = firstHalf.next;
            }
            if (secondHalf != null) {
                curr.next = secondHalf;
                curr = curr.next;
                secondHalf = secondHalf.next;
            }
        }

        // Return the new head of the rearranged list
        return dummy.next;
    }

    public static void main(String[] args){

        // singly linked list 1->2->3->4->5
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);
        head.next.next.next.next = new Node(5);

        head = rearrange(head);
        printList(head);
    }
}
Python
# Python program to rearrange link list in place

class Node:
    def __init__(self, d):
        self.data = d
        self.next = None


def printlist(node):
    
    if node is None:
        return
    
    while node is not None:
        print(node.data, end=" ")
        node = node.next

def reverselist(node):
    
    prev = None
    curr = node
    next = None
    
    while curr is not None:
        next = curr.next
        curr.next = prev
        prev = curr
        curr = next
    
    node = prev
    return node


def rearrange(node):
    
    # Find the middle point using the 
    # tortoise and hare method
    slow = node
    fast = slow.next
    
    while fast is not None and fast.next is not None:
        slow = slow.next
        fast = fast.next.next

    # Split the linked list into two halves
    node1 = node
    node2 = slow.next
    slow.next = None

    # Reverse the second half
    node2 = reverselist(node2)

    # Merge alternate nodes
    node = Node(0)  
    curr = node
    
    while node1 is not None or node2 is not None:

        # Add the element from the first list
        if node1 is not None:
            curr.next = node1
            curr = curr.next
            node1 = node1.next

        # Add the element from the second list
        if node2 is not None:
            curr.next = node2
            curr = curr.next
            node2 = node2.next

    # Return the head of the rearranged list
    return node.next

# singly linked list 1->2->3->4->5
head = None
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)

head = rearrange(head)
printlist(head)
C#
// C# program to rearrange link list in place
using System;

public class Node {
    public int Data;
    public Node Next;

    public Node(int data) {
        Data = data;
        Next = null;
    }
}

class GfG {
  
    static void PrintList(Node node) {
        if (node == null) {
            Console.WriteLine("List is empty.");
            return;
        }

        while (node != null) {
             Console.Write(node.Data + " ");
            node = node.Next;
        }
    }

    static Node ReverseList(Node node) {
        Node prev = null;
        Node curr = node;
        Node next = null;

        while (curr != null) {
            next = curr.Next;
            curr.Next = prev;
            prev = curr;
            curr = next;
        }

        return prev;
    }

    // Method to rearrange the linked list in-place and
    // return the new head
    static Node Rearrange(Node node) {
        if (node == null || node.Next == null)
            return node;

        // Find the middle point using tortoise and hare
        // method
        Node slow = node;
        Node fast = node.Next;

        while (fast != null && fast.Next != null) {
            slow = slow.Next;
            fast = fast.Next.Next;
        }

        // Split the linked list into two halves
        Node firstHalf = node;
        Node secondHalf = slow.Next;
        slow.Next = null;

        // Reverse the second half
        secondHalf = ReverseList(secondHalf);

        // Merge alternate nodes
        Node dummy = new Node(0);
        Node curr = dummy;

        while (firstHalf != null || secondHalf != null) {
            if (firstHalf != null) {
                curr.Next = firstHalf;
                curr = curr.Next;
                firstHalf = firstHalf.Next;
            }

            if (secondHalf != null) {
                curr.Next = secondHalf;
                curr = curr.Next;
                secondHalf = secondHalf.Next;
            }
        }

        // Return the new head of the list
        return dummy.Next;
    }

    public static void Main(string[] args) {
      
           //singly linked list 1->2->3->4->5
        Node head = new Node(1);
        head.Next = new Node(2);
        head.Next.Next = new Node(3);
        head.Next.Next.Next = new Node(4);
        head.Next.Next.Next.Next = new Node(5);
      
        head = Rearrange(head);
        PrintList(head);
    }
}
JavaScript
// Javascript program to rearrange link list in place
class Node {
    constructor(d) {
        this.data = d;
        this.next = null;
    }
}

function printList(node) {
    let result = "";
    while (node != null) {
        result += node.data + " ";
        node = node.next;
    }
    console.log(result);
}

function reverseList(node) {
    let prev = null, curr = node, next;
    while (curr != null) {
        next = curr.next;
        curr.next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
}

// Function to rearrange the linked list
function rearrange(head) {
    if (head == null || head.next == null) {
        return head;
    }

    // Find the middle point using 
    // tortoise and hare method
    let slow = head, fast = slow.next;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
    }

    // Split the linked list into two halves
    let node1 = head;
    let node2 = slow.next;
    slow.next = null;

    // Reverse the second half
    node2 = reverseList(node2);

    // Merge alternate nodes
    let dummy = new Node(0);
    let curr = dummy;
    while (node1 != null || node2 != null) {
        if (node1 != null) {
            curr.next = node1;
            curr = curr.next;
            node1 = node1.next;
        }

        if (node2 != null) {
            curr.next = node2;
            curr = curr.next;
            node2 = node2.next;
        }
    }

    return dummy.next;
}

// singly linked list 1->2->3->4->5
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);

head = rearrange(head);

printList(head);

Output
1 5 2 4 3 

Time Complexity: O(n) , where n are the number of nodes in linked list.
Auxiliary Space: O(1)



Similar Reads

Javascript Program For Rearranging A Given Linked List In-Place
Given a singly linked list L0 -&gt; L1 -&gt; â€Ķ -&gt; Ln-1 -&gt; Ln. Rearrange the nodes in the list so that the new formed list is : L0 -&gt; Ln -&gt; L1 -&gt; Ln-1 -&gt; L2 -&gt; Ln-2 ...You are required to do this in place without altering the nodes' values. Examples: Input: 1 -&gt; 2 -&gt; 3 -&gt; 4Output: 1 -&gt; 4 -&gt; 2 -&gt; 3Input: 1 -&gt;
7 min read
Multiply a single digit number in place to a number represented as a Linked List
Given a linked list of N nodes where each node represents digits of a number and a single-digit number M, the task is to multiply the list by M in-place and print the resulting linked list. Examples: Input: Linked list: 1 ? 2 ? 7 ? 3 ? NULL, M = 3Output: 3 ? 8 ? 1 ? 9 ? NULLExplanation: The given linked list represents the number 1273. Multiplying
12 min read
In-place Merge two linked lists without changing links of first list
Given two sorted singly linked lists having n and m elements each, merge them using constant space. First n smallest elements in both the lists should become part of first list and rest elements should be part of second list. Sorted order should be maintained. We are not allowed to change pointers of first linked list. For example, Input: First Lis
11 min read
Rearrange a linked list in to alternate first and last element
Given a linked list. arrange the linked list in manner of alternate first and last element. Examples: Input : 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8 Output :1-&gt;8-&gt;2-&gt;7-&gt;3-&gt;6-&gt;4-&gt;5 Input :10-&gt;11-&gt;15-&gt;13 Output :10-&gt;13-&gt;11-&gt;15 We have discussed three different solution in Rearrange a given linked list in-pl
8 min read
Rearrange a Linked List in Zig-Zag fashion | Set-2
Given a linked list, rearrange it such that converted list should be of the form a &lt; b &gt; c &lt; d &gt; e &lt; f .. where a, b, c.. are consecutive data node of linked list. Note that it is not allowed to swap data. Examples: Input: 1-&gt;2-&gt;3-&gt;4 Output: 1-&gt;3-&gt;2-&gt;4 Input: 11-&gt;15-&gt;20-&gt;5-&gt;10 Output: 11-&gt;20-&gt;5-
13 min read
Rearrange Linked List to make XOR of nodes at same distance from start and end same
Given a linked list containing N nodes of binary numbers, the task is to check whether it is possible to rearrange the linked list in such a way that value of XOR between element at ith Node and N+1−ith node is the same for all 1 â‰Ī i â‰Ī N. Print Yes, if the Linked List can be rearranged, or else print No. Examples: Input: LL = 0 -&gt; 0 -&gt; 1Outpu
8 min read
Rearrange a linked list such that all even and odd positioned nodes are together
Rearrange a linked list in such a way that all odd position nodes are together and all even positions node are together, Examples: Input: 1-&gt;2-&gt;3-&gt;4Output: 1-&gt;3-&gt;2-&gt;4Input: 10-&gt;22-&gt;30-&gt;43-&gt;56-&gt;70Output: 10-&gt;30-&gt;56-&gt;22-&gt;43-&gt;70The important thing in this question is to make sure that all below cases are
15+ min read
Rearrange a Linked List in Zig-Zag fashion
Given a linked list, rearrange it such that the converted list should be of the form a &lt; b &gt; c &lt; d &gt; e &lt; f â€Ķ where a, b, câ€Ķ are consecutive data nodes of the linked list. Examples: Input: 1-&gt;2-&gt;3-&gt;4 Output: 1-&gt;3-&gt;2-&gt;4 Explanation : 1 and 3 should come first before 2 and 4 in zig-zag fashion, So resultant linked-list
15+ min read
XOR Linked List - Reverse a Linked List in groups of given size
Given a XOR linked list and an integer K, the task is to reverse every K nodes in the given XOR linked list. Examples: Input: XLL = 7&lt; – &gt; 6 &lt; – &gt; 8 &lt; – &gt; 11 &lt; – &gt; 3, K = 3 Output: 8 &lt; – &gt; 6 &lt; – &gt; 7 &lt; – &gt; 3 &lt; – &gt; 11 Explanation: Reversing first K(= 3) nodes modifies the Linked List to 8 &lt; – &gt; 6
13 min read
XOR Linked List - Pairwise swap elements of a given linked list
Given a XOR linked list, the task is to pairwise swap the elements of the given XOR linked list . Examples: Input: 4 &lt;-&gt; 7 &lt;-&gt; 9 &lt;-&gt; 7Output: 7 &lt;-&gt; 4 &lt;-&gt; 7 &lt;-&gt; 9Explanation:First pair of nodes are swapped to formed the sequence {4, 7} and second pair of nodes are swapped to form the sequence {9, 7}. Input: 1 &lt;
12 min read
Create new linked list from two given linked list with greater element at each node
Given two linked list of the same size, the task is to create a new linked list using those linked lists. The condition is that the greater node among both linked list will be added to the new linked list.Examples: Input: list1 = 5-&gt;2-&gt;3-&gt;8list2 = 1-&gt;7-&gt;4-&gt;5Output: New list = 5-&gt;7-&gt;4-&gt;8Input: list1 = 2-&gt;8-&gt;9-&gt;3li
8 min read
Rearrange a given list such that it consists of alternating minimum maximum elements
Given a list of integers, rearrange the list such that it consists of alternating minimum maximum elements using only list operations. The first element of the list should be minimum and second element should be maximum of all elements present in the list. Similarly, third element will be next minimum element and fourth element is next maximum elem
9 min read
Minimum cost required to rearrange a given array to make it equal to another given array
Given two arrays A[] and B[] consisting of M and N integers respectively, and an integer C, the task is to find the minimum cost required to make the sequence A exactly the same as B(consists of distinct elements only) by performing the following operations on array A[]: Remove any element from the array with cost 0.Insert a new element anywhere in
12 min read
Rearrange two given arrays such that sum of same indexed elements lies within given range
Given two arrays arr1[] and arr2[] consisting of N positive integers and an even integer K, the task is to check if the sum of the same indexed elements in the two arrays lie in the range [K/2, K] after rearranging the given arrays or not. If it is possible to obtain such an arrangement, then print "Yes". Otherwise, print "No". Examples: Input: arr
6 min read
Convert Singly Linked List to XOR Linked List
Prerequisite: XOR Linked List – A Memory Efficient Doubly Linked List | Set 1XOR Linked List – A Memory Efficient Doubly Linked List | Set 2 An XOR linked list is a memory efficient doubly linked list in which the next pointer of every node stores the XOR of previous and next node's address. Given a singly linked list, the task is to convert the gi
9 min read
XOR linked list- Remove first node of the linked list
Given an XOR linked list, the task is to remove the first node of the XOR linked list. Examples: Input: XLL = 4 &lt; – &gt; 7 &lt; – &gt; 9 &lt; – &gt; 7 Output: 7 &lt; – &gt; 9 &lt; – &gt; 7 Explanation: Removing the first node of the XOR linked list modifies XLL to 7 &lt; – &gt; 9 &lt; – &gt; 7 Input: XLL = NULL Output: List Is Empty Approach: Th
11 min read
Remove all occurrences of one Linked list in another Linked list
Given two linked lists head1 and head2, the task is to remove all occurrences of head2 in head1 and return the head1. Examples: Input: head1 = 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 3 -&gt; 4, head2 = 3 -&gt; 4Output: 2 -&gt; 5Explanation: After removing all occurrences of 3 -&gt; 4 in head1 output is 2 -&gt; 5. Input: head1 = 3 -&gt; 6 -&gt; 9 -&gt; 8 -
9 min read
Count occurrences of one linked list in another Linked List
Given two linked lists head1 and head2, the task is to find occurrences of head2 in head1. Examples: Input: Head1 = 1-&gt;2-&gt;3-&gt;2-&gt;4-&gt;5-&gt;2-&gt;4, Head2 = 2-&gt;4Output: Occurrences of head2 in head1: 2 Input: Head1 = 3-&gt;4-&gt;1-&gt;5-&gt;2, Head2 = 3-&gt;4Output: Occurrences of Head2 in Head1: 1 Approach 1: To solve the problem us
15 min read
When is Doubly Linked List more Efficient than Singly Linked List?
Did you know there are some cases where a Doubly Linked List is more efficient than a Singly Linked List, even though it takes more memory compared to a Singly Linked List? What are those Cases? Well, we will discuss that in the following article, But first, let's talk about Singly and linked lists: What is a Singly Linked List?A singly linked list
4 min read
XOR Linked List: Remove last node of the Linked List
Given an XOR linked list, the task is to delete the node at the end of the XOR Linked List. Examples: Input: 4&lt;–&gt;7&lt;–&gt;9&lt;–&gt;7Output: 4&lt;–&gt;7&lt;–&gt;9Explanation: Deleting a node from the end modifies the given XOR Linked List to 4&lt;–&gt;7&lt;–&gt;9 Input: 10Output: List is emptyExplanation: After deleting the only node present
15+ min read
Is two way linked list and doubly linked list same?
Yes, a two-way linked list and a doubly linked list are the same. Both terms refer to a type of linked list where each node contains a reference to the next node as well as the previous node in the sequence. The term “two-way” emphasizes the ability to move in both directions through the list, while “doubly” highlights that there are two links per
3 min read
XOR linked list: Reverse last K nodes of a Linked List
Given a XOR Linked List and a positive integer K, the task is to reverse the last K nodes in the given XOR linked list. Examples: Input: LL: 7 &lt;–&gt; 6 &lt;–&gt; 8 &lt;–&gt; 11 &lt;–&gt; 3 &lt;–&gt; 1, K = 3Output: 7&lt;–&gt;6&lt;–&gt;8&lt;–&gt;1&lt;–&gt;3&lt;–&gt;11 Input: LL: 7 &lt;–&gt; 6 &lt;–&gt; 8 &lt;–&gt; 11 &lt;–&gt; 3 &lt;–&gt; 1 &lt;–
14 min read
XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
In this post, we're going to talk about how XOR linked lists are used to reduce the memory requirements of doubly-linked lists. We know that each node in a doubly-linked list has two pointer fields which contain the addresses of the previous and next node. On the other hand, each node of the XOR linked list requires only a single pointer field, whi
15+ min read
Linked List of Linked List
Linked list of linke­d list, also known as nested linke­d lists, is a type of Linked List where each main node stores another full linked list. This structure­ beats old, plain linked lists. It gives way more­ flexibility to store and manage comple­x data. In this article we will learn about the basics of Linked List of Linked List, how to create L
9 min read
XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
In the previous post, we discussed how a Doubly Linked can be created using only one space for the address field with every node. In this post, we will discuss the implementation of a memory-efficient doubly linked list. We will mainly discuss the following two simple functions. A function to insert a new node at the beginning.A function to travers
10 min read
Difference between Singly linked list and Doubly linked list
Introduction to Singly linked list : A singly linked list is a set of nodes where each node has two fields 'data' and 'link'. The 'data' field stores actual piece of information and 'link' field is used to point to next node. Basically the 'link' field stores the address of the next node.  Introduction to Doubly linked list : A Doubly Linked List (
2 min read
Check if a linked list is Circular Linked List
Given the head of a singly linked list, the task is to find if given linked list is circular or not. A linked list is called circular if its last node points back to its first node. Note: The linked list does not contain any internal loops. Example: Input: LinkedList: 2-&gt;4-&gt;6-&gt;7-&gt;5 Output: trueExplanation: As shown in figure the first a
12 min read
Javascript Program To Merge A Linked List Into Another Linked List At Alternate Positions
Given two linked lists, insert nodes of the second list into the first list at alternate positions of the first list. For example, if first list is 5-&gt;7-&gt;17-&gt;13-&gt;11 and second is 12-&gt;10-&gt;2-&gt;4-&gt;6, the first list should become 5-&gt;12-&gt;7-&gt;10-&gt;17-&gt;2-&gt;13-&gt;4-&gt;11-&gt;6 and second list should become empty. The
3 min read
Insert a linked list into another linked list
Given two linked lists, head1 and head2 of sizes m and n respectively. The task is to remove head1's nodes from the ath node to the bth node and insert head2 in their place. Examples: Input: a = 3, b = 4head1: 10 -&gt; 11 -&gt; 12 -&gt; 13 -&gt; 14 -&gt; 15, head2: 100 -&gt; 101 -&gt; 102 -&gt; 103Output: 10 -&gt; 11 -&gt; 12 -&gt; 100 -&gt; 101 -
10 min read
Convert singly linked list into circular linked list
Given a singly linked list, the task is to convert it into a circular linked list. Examples: Input: head: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6 -&gt; NULLOutput: Explanation: Singly linked list is converted to circular by pointing last node to head. Input: head: 2 -&gt; 4 -&gt; 6 -&gt; 8 -&gt; 10 -&gt; 12 -&gt; NULLOutput: Explanation: Singly l
11 min read
Article Tags :
Practice Tags :