Append the last M nodes to the beginning of the given linked list.
Given a linked list and an integer M, the task is to append the last M nodes of the linked list to the front.
Examples:
Input: List = 4 -> 5 -> 6 -> 1 -> 2 -> 3 -> NULL, M = 3
Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL
Input: List = 8 -> 7 -> 0 -> 4 -> 1 -> NULL, M = 2
Output: 4 -> 1 -> 8 -> 7 -> 0 -> NULL
Approach: Find the first node of the last M nodes in the list, this node will be the new head node so make the next pointer of the previous node as NULL and point the last node of the original list to the head of the original list. Finally, print the updated list.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
Node( int data)
{
this ->data = data;
this ->next = NULL;
}
};
void printList(Node* node)
{
while (node != NULL) {
cout << (node->data) << " -> " ;
node = node->next;
}
cout << "NULL" ;
}
int cntNodes(Node* node)
{
if (node == NULL)
return 0;
return (1 + cntNodes(node->next));
}
void updateList(Node* head, int m)
{
int cnt = cntNodes(head);
if (cnt != m && m < cnt) {
int skip = cnt - m;
Node* prev = NULL;
Node* curr = head;
while (skip > 0) {
prev = curr;
curr = curr->next;
skip--;
}
prev->next = NULL;
Node* tempHead = head;
head = curr;
while (curr->next != NULL)
curr = curr->next;
curr->next = tempHead;
}
printList(head);
}
int main()
{
Node* head = new Node(4);
head->next = new Node(5);
head->next->next = new Node(6);
head->next->next->next = new Node(1);
head->next->next->next->next = new Node(2);
head->next->next->next->next->next = new Node(3);
int m = 3;
updateList(head, m);
return 0;
}
|
Java
class GFG {
static class Node {
int data;
Node next;
Node( int data)
{
this .data = data;
this .next = null ;
}
}
static void printList(Node node)
{
while (node != null ) {
System.out.print(node.data + " -> " );
node = node.next;
}
System.out.print( "NULL" );
}
static int cntNodes(Node node)
{
if (node == null )
return 0 ;
return ( 1 + cntNodes(node.next));
}
static void updateList(Node head, int m)
{
int cnt = cntNodes(head);
if (cnt != m && m < cnt) {
int skip = cnt - m;
Node prev = null ;
Node curr = head;
while (skip > 0 ) {
prev = curr;
curr = curr.next;
skip--;
}
prev.next = null ;
Node tempHead = head;
head = curr;
while (curr.next != null )
curr = curr.next;
curr.next = tempHead;
}
printList(head);
}
public static void main(String[] args)
{
Node head = new Node( 4 );
head.next = new Node( 5 );
head.next.next = new Node( 6 );
head.next.next.next = new Node( 1 );
head.next.next.next.next = new Node( 2 );
head.next.next.next.next.next = new Node( 3 );
int m = 3 ;
updateList(head, m);
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self . next = None
def printList(node):
while (node ! = None ):
print (node.data, "->" , end = " " )
node = node. next
print ( "NULL" )
def cntNodes(node):
if (node = = None ):
return 0
return ( 1 + cntNodes(node. next ))
def updateList(head, m):
cnt = cntNodes(head)
if (cnt ! = m and m < cnt):
skip = cnt - m
prev = None
curr = head
while (skip > 0 ):
prev = curr
curr = curr. next
skip - = 1
prev. next = None
tempHead = head
head = curr
while (curr. next ! = None ):
curr = curr. next
curr. next = tempHead
printList(head)
head = newNode( 4 )
head. next = newNode( 5 )
head. next . next = newNode( 6 )
head. next . next . next = newNode( 1 )
head. next . next . next . next = newNode( 2 )
head. next . next . next . next . next = newNode( 3 )
m = 3
updateList(head, m)
|
C#
using System;
class GFG {
class Node {
public int data;
public Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
static void printList(Node node)
{
while (node != null ) {
Console.Write(node.data + " -> " );
node = node.next;
}
Console.Write( "NULL" );
}
static int cntNodes(Node node)
{
if (node == null )
return 0;
return (1 + cntNodes(node.next));
}
static void updateList(Node head, int m)
{
int cnt = cntNodes(head);
if (cnt != m && m < cnt) {
int skip = cnt - m;
Node prev = null ;
Node curr = head;
while (skip > 0) {
prev = curr;
curr = curr.next;
skip--;
}
prev.next = null ;
Node tempHead = head;
head = curr;
while (curr.next != null )
curr = curr.next;
curr.next = tempHead;
}
printList(head);
}
public static void Main(String[] args)
{
Node head = new Node(4);
head.next = new Node(5);
head.next.next = new Node(6);
head.next.next.next = new Node(1);
head.next.next.next.next = new Node(2);
head.next.next.next.next.next = new Node(3);
int m = 3;
updateList(head, m);
}
}
|
Javascript
<script>
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function printList(node) {
while (node != null ) {
document.write(node.data + " -> " );
node = node.next;
}
document.write( "NULL" );
}
function cntNodes(node) {
if (node == null ) return 0;
return 1 + cntNodes(node.next);
}
function updateList(head, m) {
var cnt = cntNodes(head);
if (cnt != m && m < cnt) {
var skip = cnt - m;
var prev = null ;
var curr = head;
while (skip > 0) {
prev = curr;
curr = curr.next;
skip--;
}
prev.next = null ;
var tempHead = head;
head = curr;
while (curr.next != null ) curr = curr.next;
curr.next = tempHead;
}
printList(head);
}
var head = new Node(4);
head.next = new Node(5);
head.next.next = new Node(6);
head.next.next.next = new Node(1);
head.next.next.next.next = new Node(2);
head.next.next.next.next.next = new Node(3);
var m = 3;
updateList(head, m);
</script>
|
Output
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL
METHOD 2:
We Will use modification of runner’s technique :-
1. find the kth node from end using runner technique and do the following modifications
2. now we have to update our pointers as
a) fast->next will be pointing to head,
b)slow->next will be new head,
c)last node will be the slow->next hence it should point to null
C++
#include <iostream>
using namespace std;
struct node {
int data;
node* next;
node( int x)
{
data = x;
next = NULL;
}
};
void insertAtTail(node*& head, int x)
{
if (head == NULL) {
head = new node(x);
return ;
}
node* curr = head;
while (curr->next != NULL) {
curr = curr->next;
}
node* t = new node(x);
curr->next = t;
}
void print(node* head)
{
node* curr = head;
while (curr != NULL) {
cout << curr->data << " -> " ;
curr = curr->next;
}
cout << "NULL\n" ;
}
node* appendK(node* head, int k)
{
node* fast = head;
node* slow = head;
for ( int i = 0; i < k; i++) {
fast = fast->next;
}
while (fast->next != NULL) {
slow = slow->next;
fast = fast->next;
}
fast->next = head;
head = slow->next;
slow->next = NULL;
return head;
}
int main()
{
node* head = NULL;
int n;
n = 6;
insertAtTail(head, 4);
insertAtTail(head, 5);
insertAtTail(head, 6);
insertAtTail(head, 1);
insertAtTail(head, 2);
insertAtTail(head, 3);
int k;
k = 3;
head = appendK(head, k % n);
print(head);
return 0;
}
|
Java
import java.io.*;
class GFG {
class Node {
int data;
Node next;
Node( int x)
{
data = x;
next = null ;
}
}
public Node insertAtTail(Node head, int x)
{
if (head == null ) {
head = new Node(x);
return head;
}
Node curr = head;
while (curr.next != null ) {
curr = curr.next;
}
Node t = new Node(x);
curr.next = t;
return head;
}
public void print(Node head)
{
Node curr = head;
while (curr != null ) {
System.out.print(curr.data + " -> " );
curr = curr.next;
}
System.out.print( "NULL\n" );
}
public Node appendK(Node head, int k)
{
Node fast = head;
Node slow = head;
for ( int i = 0 ; i < k; i++) {
fast = fast.next;
}
while (fast.next != null ) {
slow = slow.next;
fast = fast.next;
}
fast.next = head;
head = slow.next;
slow.next = null ;
return head;
}
public static void main(String[] args)
{
GFG l = new GFG();
Node head = null ;
int n = 6 ;
head = l.insertAtTail(head, 4 );
head = l.insertAtTail(head, 5 );
head = l.insertAtTail(head, 6 );
head = l.insertAtTail(head, 1 );
head = l.insertAtTail(head, 2 );
head = l.insertAtTail(head, 3 );
int k = 3 ;
head = l.appendK(head, k % n);
l.print(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def insertAtTail( self , head, x):
if head is None :
head = Node(x)
return head
curr = head
while curr. next is not None :
curr = curr. next
t = Node(x)
curr. next = t
return head
def printList( self , head):
curr = head
while curr is not None :
print (curr.data, end = " -> " )
curr = curr. next
print ( "NULL" )
def appendK( self , head, k):
fast = head
slow = head
for i in range (k):
fast = fast. next
while fast. next is not None :
slow = slow. next
fast = fast. next
fast. next = head
head = slow. next
slow. next = None
return head
if __name__ = = "__main__" :
l = LinkedList()
head = None
n = 6
head = l.insertAtTail(head, 4 )
head = l.insertAtTail(head, 5 )
head = l.insertAtTail(head, 6 )
head = l.insertAtTail(head, 1 )
head = l.insertAtTail(head, 2 )
head = l.insertAtTail(head, 3 )
k = 3
head = l.appendK(head, k % n)
l.printList(head)
|
Javascript
class Node {
constructor(x){
this .data = x;
this .next = null ;
}
}
function insertAtTail( head, x)
{
if (head == null ) {
head = new Node(x);
return head;
}
let curr = head;
while (curr.next != null ) {
curr = curr.next;
}
let t = new Node(x);
curr.next = t;
return head;
}
function print(head)
{
let curr = head;
while (curr != null ) {
process.stdout.write(curr.data + " -> " );
curr = curr.next;
}
console.log( "NULL" );
}
function appendK(head, k)
{
let fast = head;
let slow = head;
for (let i = 0; i < k; i++) {
fast = fast.next;
}
while (fast.next != null ) {
slow = slow.next;
fast = fast.next;
}
fast.next = head;
head = slow.next;
slow.next = null ;
return head;
}
let head = null ;
let n = 6;
head = insertAtTail(head, 4);
head = insertAtTail(head, 5);
head = insertAtTail(head, 6);
head = insertAtTail(head, 1);
head = insertAtTail(head, 2);
head = insertAtTail(head, 3);
let k = 3;
head = appendK(head, k % n);
print(head);
|
C#
using System;
public class GFG {
class Node {
public int data;
public Node next;
public Node( int x)
{
data = x;
next = null ;
}
}
Node insertAtTail(Node head, int x)
{
if (head == null ) {
head = new Node(x);
return head;
}
Node curr = head;
while (curr.next != null ) {
curr = curr.next;
}
Node t = new Node(x);
curr.next = t;
return head;
}
void print(Node head)
{
Node curr = head;
while (curr != null ) {
Console.Write(curr.data + " -> " );
curr = curr.next;
}
Console.Write( "NULL\n" );
}
Node appendK(Node head, int k)
{
Node fast = head;
Node slow = head;
for ( int i = 0; i < k; i++) {
fast = fast.next;
}
while (fast.next != null ) {
slow = slow.next;
fast = fast.next;
}
fast.next = head;
head = slow.next;
slow.next = null ;
return head;
}
static public void Main()
{
GFG l = new GFG();
Node head = null ;
int n = 6;
head = l.insertAtTail(head, 4);
head = l.insertAtTail(head, 5);
head = l.insertAtTail(head, 6);
head = l.insertAtTail(head, 1);
head = l.insertAtTail(head, 2);
head = l.insertAtTail(head, 3);
int k = 3;
head = l.appendK(head, k % n);
l.print(head);
}
}
|
Output
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL
Time complexity: O(n) where n is size of given linked list
Last Updated :
22 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...