Move last element to front of a given Linked List | Set 2
Given a singly linked list and an integer K. The task is to append last K elements of the linked list to front.
Examples:
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6, k = 3
Output : 4 -> 5 -> 6 -> 1 -> 2 -> 3
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6, k = 7
Output : 6 -> 1 -> 2 -> 3 -> 4 -> 5
Prerequisites: Move last element to front of a given Linked List
Approach:
- Loop over (k % n) times, Where n is the number of elements of the linked list.
- Each time, delete one node from the end of the linked list.
- Simultaneously insert that deleted node at the beginning of the linked list.
- Find the middle of a given linked list in C and Java
- Program for n'th node from the end of a Linked List
- Write a function to get Nth node in a Linked List
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Detect loop in a linked list
- Write a function to delete a Linked List
- Write a function that counts the number of times a given int occurs in a Linked List
- Reverse a linked list
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- Write a function to get the intersection point of two Linked Lists
- Function to check if a singly linked list is palindrome
- The Great Tree-List Recursion Problem.
- Clone a linked list with next and random pointer | Set 1
- Memory efficient doubly linked list
- Given a linked list which is sorted, how will you insert in sorted way
Below is the implementation of the above approach :
// CPP Program to move k last elements // to front in a given linked list #include<iostream> using namespace std; // A linked list node class Node { public: int data; Node* next; Node(int d) { data = d; next = NULL; } }; // Function to add a new node at the // end/tail of Linked List void insertAtTail(Node*& head, Node*& tail, int d ) { Node* newnode = new Node(d); if(tail == NULL) { tail = head = newnode; return; } tail->next = newnode; tail = newnode; } // Function to add a node at the // beginning of Linked List void insertAtHead(Node*& head, Node*& tail, Node*& deletedNode) { if(head == NULL) { head = tail = deletedNode; return; } deletedNode->next = head; head = deletedNode; return; } // Function to add a node at the beginning of Linked List Node* deleteAtTail(Node*& head, Node*& tail) { Node* deleted = tail; Node* temp = head; while(temp->next->next != NULL) { temp = temp->next; } temp->next = NULL; tail = temp; return deleted; } // k can be more than n, so this function takes the modulus // of k with n and delete nodes from end k // times and simultaneously insert it in front void appendAtFront(Node*& head, Node*& tail, int n, int k) { k = k % n; while(k != 0) { Node* deleted = deleteAtTail(head, tail); insertAtHead(head, tail, deleted); k--; } } // Function to print nodes in a given linked list void printLinkedList(Node* head) { while(head != NULL) { cout << head->data << " "; head = head->next; } cout << endl; } // Driver Code int main() { // Pointer to the start of the linked list Node* head = NULL; // Pointer to the end of the linked list Node* tail = NULL; // Number of elements in the linked list int n = 6; // Building linked list insertAtTail(head, tail, 1); insertAtTail(head, tail, 2); insertAtTail(head, tail, 3); insertAtTail(head, tail, 4); insertAtTail(head, tail, 5); insertAtTail(head, tail, 6); // Printing linked list before // appending the linked list cout << "Linked List before appending: "; printLinkedList(head); // Number of elements to be appended int k = 7; // Function call appendAtFront(head, tail, n, k); // Printing linked list after appending the list cout << "Linked List after appending "<<k<<" elements: "; printLinkedList(head); } |
Linked List before appending: 1 2 3 4 5 6 Linked List after appending 7 elements: 6 1 2 3 4 5
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



