
- 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
Move first element to end of a given Linked List in C++
<
Given a linked list, we have to move the first element to the end. Let's see an example.
Input
1 -> 2 -> 3 -> 4 -> 5 -> NULL
Output
2 -> 3 -> 4 -> 5 -> 1 -> NULL
Algorithm
Initialise the linked list.
- Return if the linked list is empty or it has single node.
Find the last node of the linked list.
Make the second node as the new head.
Update the links of first and last nodes.
Implementation
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node* next; }; void moveFirstNodeToEnd(struct Node** head) { if (*head == NULL || (*head)->next == NULL) { return; } struct Node* firstNode = *head; struct Node* lastNode = *head; while (lastNode->next != NULL) { lastNode = lastNode->next; } *head = firstNode->next; firstNode->next = NULL; lastNode->next = firstNode; } void addNewNode(struct Node** head, int new_data) { struct Node* newNode = new Node; newNode->data = new_data; newNode->next = *head; *head = newNode; } void printLinkedList(struct Node* node) { while (node != NULL) { cout << node->data << "->"; node = node->next; } cout << "NULL" << endl; } int main() { struct Node* head = NULL; addNewNode(&head, 1); addNewNode(&head, 2); addNewNode(&head, 3); addNewNode(&head, 4); addNewNode(&head, 5); addNewNode(&head, 6); addNewNode(&head, 7); addNewNode(&head, 8); addNewNode(&head, 9); moveFirstNodeToEnd(&head); printLinkedList(head); return 0; }
Output
If you run the above code, then you will get the following result.
8->7->6->5->4->3->2->1->9->NULL
- Related Articles
- Move last element to front of a given Linked List in C++
- Double the first element and move zero to end in C++ Program
- Golang Program to add a node at the end of a given linked list.
- Golang Program to add the first node in a given linked list.
- In-place Move Zeros to End of List in Python
- Java Program to Add Element at First and Last Position of a Linked list
- Python Program to add element to first and last position of linked list
- C++ Program to Delete the First Node in a given Singly Linked List
- In-place Algorithm to Move Zeros to End of List in JavaScript
- Find the Kth Node from the end in a given singly Linked List using C++
- Move the first row to the end of the JTable in Java Swing
- Move all zeros to the front of the linked list in C++
- Find the product of first k nodes of the given Linked List in C++
- Search an element in the given singly Linked List using C++
- Program to insert new element into a linked list before the given position in Python

Advertisements