Reverse first K elements of given linked list
Last Updated :
22 Sep, 2022
Given a pointer to the head node of a linked list and a number K, the task is to reverse the first K nodes of the linked list. We need to reverse the list by changing links between nodes.
check also Reversal of a linked list
Examples:
Input : 1->2->3->4->5->6->7->8->9->10->NULL, k = 3
Output : 3->2->1->4->5->6->7->8->9->10->NULL
Input : 10->18->20->25->35->NULL, k = 2
Output : 18->10->20->25->35->NULL
Explanation of the method:
suppose linked list is 1->2->3->4->5->NULL and k=3
1) Traverse the linked list till K-th point.
2) Break the linked list in to two parts from k-th point. After partition linked list will look like 1->2->3->NULL & 4->5->NULL
3) Reverse first part of the linked list leave second part as it is 3->2->1->NULL and 4->5->NULL
4) Join both the parts of the linked list, we get 3->2->1->4->5->NULL
A pictorial representation of how the algorithm works
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
static void reverseKNodes( struct Node** head_ref, int k)
{
struct Node* temp = *head_ref;
int count = 1;
while (count < k) {
temp = temp->next;
count++;
}
struct Node* joint_point = temp->next;
temp->next = NULL;
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
current = *head_ref;
while (current->next != NULL)
current = current->next;
current->next = joint_point;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node =
( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList( struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
printf ( "%d " , temp->data);
temp = temp->next;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
int k = 3;
cout << "\nGiven list\n" ;
printList(head);
reverseKNodes(&head, k);
cout << "\nModified list\n" ;
printList(head);
return 0;
}
|
Java
class Sol
{
static class Node
{
int data;
Node next;
};
static Node reverseKNodes( Node head_ref, int k)
{
Node temp = head_ref;
int count = 1 ;
while (count < k)
{
temp = temp.next;
count++;
}
Node joint_point = temp.next;
temp.next = null ;
Node prev = null ;
Node current = head_ref;
Node next;
while (current != null )
{
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head_ref = prev;
current = head_ref;
while (current.next != null )
current = current.next;
current.next = joint_point;
return head_ref;
}
static Node push( Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static void printList( Node head)
{
Node temp = head;
while (temp != null )
{
System.out.printf( "%d " , temp.data);
temp = temp.next;
}
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 5 );
head = push(head, 4 );
head = push(head, 3 );
head = push(head, 2 );
head = push(head, 1 );
int k = 3 ;
System.out.print( "\nGiven list\n" );
printList(head);
head = reverseKNodes(head, k);
System.out.print( "\nModified list\n" );
printList(head);
}
}
|
Python
class Node:
def __init__( self , next = None , data = None ):
self . next = next
self .data = data
def reverseKNodes(head_ref, k) :
temp = head_ref
count = 1
while (count < k):
temp = temp. next
count = count + 1
joint_point = temp. next
temp. next = None
prev = None
current = head_ref
next = None
while (current ! = None ):
next = current. next
current. next = prev
prev = current
current = next
head_ref = prev
current = head_ref
while (current. next ! = None ):
current = current. next
current. next = joint_point
return head_ref
def push(head_ref, new_data) :
new_node = Node()
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
def printList( head) :
temp = head
while (temp ! = None ):
print (temp.data, end = " " )
temp = temp. next
head = None
head = push(head, 5 )
head = push(head, 4 )
head = push(head, 3 )
head = push(head, 2 )
head = push(head, 1 )
k = 3
print ( "\nGiven list" )
printList(head)
head = reverseKNodes(head, k)
print ( "\nModified list" )
printList(head)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node reverseKNodes(Node head_ref, int k)
{
Node temp = head_ref;
int count = 1;
while (count < k)
{
temp = temp.next;
count++;
}
Node joint_point = temp.next;
temp.next = null ;
Node prev = null ;
Node current = head_ref;
Node next;
while (current != null )
{
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head_ref = prev;
current = head_ref;
while (current.next != null )
current = current.next;
current.next = joint_point;
return head_ref;
}
static Node push( Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static void printList( Node head)
{
Node temp = head;
while (temp != null )
{
Console.Write( "{0} " , temp.data);
temp = temp.next;
}
}
public static void Main(String []args)
{
Node head = null ;
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
int k = 3;
Console.Write( "Given list\n" );
printList(head);
head = reverseKNodes(head, k);
Console.Write( "\nModified list\n" );
printList(head);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
};
function reverseKNodes(head_ref, k)
{
var temp = head_ref;
var count = 1;
while (count < k)
{
temp = temp.next;
count++;
}
var joint_point = temp.next;
temp.next = null ;
var prev = null ;
var current = head_ref;
var next;
while (current != null )
{
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head_ref = prev;
current = head_ref;
while (current.next != null )
current = current.next;
current.next = joint_point;
return head_ref;
}
function push( head_ref, new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
function printList( head)
{
var temp = head;
while (temp != null )
{
document.write(temp.data+ " " );
temp = temp.next;
}
}
var head = null ;
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
var k = 3;
document.write( "Given list<br>" );
printList(head);
head = reverseKNodes(head, k);
document.write( "<br>Modified list<br>" );
printList(head);
</script>
|
Output:
Given list
1 2 3 4 5
Modified list
3 2 1 4 5
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...