
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to rotate a linked list by k places in C++
Suppose we have a linked list. We have to rotate the list to the right by k places. The value of k will be positive. So if the list is like [1 −> 2 −> 3 −> 4 −> 5 −> NULL], and k = 2, then the output will be [4 −> 5 −> 1 −> 2 −> 3 −> NULL]
Let us see the steps −
If the list is empty, then return null
len := 1
create one node called tail := head
while next of tail is not null
increase len by 1
tail := next of tail
next of tail := head
k := k mod len
newHead := null
for i := 0 to len − k
tail := next of tail
newHead := next of tail
next of tail := null
return newHead
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class ListNode{ public: int val; ListNode *next; ListNode(int data){ val = data; next = NULL; } }; ListNode *make_list(vector<int> v){ ListNode *head = new ListNode(v[0]); for(int i = 1; i<v.size(); i++){ ListNode *ptr = head; while(ptr->next != NULL){ ptr = ptr->next; } ptr->next = new ListNode(v[i]); } return head; } void print_list(ListNode *head){ ListNode *ptr = head; cout << "["; while(ptr->next){ cout << ptr->val << ", "; ptr = ptr->next; } cout << "]" << endl; } class Solution { public: ListNode* rotateRight(ListNode* head, int k) { if(!head) return head; int len = 1; ListNode* tail = head; while(tail->next){ len++; tail = tail->next; } tail->next = head; k %= len; ListNode* newHead = NULL; for(int i = 0; i < len - k; i++){ tail = tail->next; } newHead = tail->next; tail->next = NULL; return newHead; } }; main(){ Solution ob; vector<int> v = {1,2,3,4,5,6,7,8,9}; ListNode *head = make_list(v); print_list(ob.rotateRight(head, 4)); }
Input
[1,2,3,4,5,6,7,8,9], 4
Output
[6, 7, 8, 9, 1, 2, 3, 4, ]
- Related Articles
- Python program to rotate doubly linked list by N nodes
- Rotate List Left by K in C++
- Program to reverse linked list by groups of size k in Python
- Python program to right rotate a list by n
- Program to find the K-th last node of a linked list in Python
- Program to arrange linked list nodes based on the value k in Python
- Java Program to Rotate Elements of a List
- Program to reverse a linked list in Python
- Program to find linked list intersection from two linked list in Python
- Reverse Alternate K Nodes in a Singly Linked List in C++
- Golang program to detect a loop in linked list
- Golang program to insert a new node after the Kth node (K is not in the linked list)
- Golang Program to define a singly linked list.
- Golang Program to reverse a given linked list.
- Program to find folded list from a given linked list in Python

Advertisements