C++ program to append the last n nodes of a linked list to the beginning of the list
How to append the last n nodes of a linked list to its beginning?
In this article, we will learn how we can extract those n nodes and write a C++ program to append the last n nodes of a lined list to the beginning of the list. Appending last n nodes of a linked list to its beginning can be understood in a very simple way, it means to dig out or extract last n nodes of the linked list and append then to the beginning of the list.
Steps to write a C++ program to append the last n nodes of a linked list to the beginning of the list
To append last n nodes to the beginning of the list we need to follow the following steps
- Construct nodes and initialize the variables.
- Create methods to build list
- Now create a method to display or print the list.
- Now we will extract last n nodes from the list using append function.
- In the main function, first print last n nodes and then print remaining starting nodes.
In this way last n nodes will get append to beginning of the list.
Syntax
class Node
{
int data;
Node *next;
};
Algorithm to append last n nodes to the beginning of the list
To dig out last n nodes we need to follow following algorithm.
- WHILE (LAST!=NULL && I<N-K-1)
- LAST=LAST->NEXT
- I++
- END WHILE
- NODE *SECOND = LAST->NEXT
- LAST->NEXT=NULL
- RETURN SECOND
Program to append the last n nodes of a linked list to its beginning in C++
Run
#include<iostream> using namespace std; class node { public: int data; node *next; //Constructor node (int d) { data = d; next = NULL; } }; void insertAtTail (node * &head, int data) //function to insert new element at tail of the list { if (head == NULL) { head = new node (data); return; } node *tail = head; while (tail->next != NULL) { tail = tail->next; } tail->next = new node (data); } int buildList (node * &head) //function to build the list. { int n; cout << "Enter the size of list:"; cin >> n; cout << endl; int a = n; cout << "Enter data of the nodes\n"; while (n--) { int data; cin >> data; insertAtTail (head, data); //New element will be inserted at end. } return a; } void display (node * node) { // as linked list will end when Node is Null while (node != NULL) { printf ("%d ", node->data); node = node->next; } } node *append (node * head, int k, int n) //function to find last n nodes { node *last; last = head; int i = 0; int ok = n - k - 1; while (last != NULL && i < ok) { last = last->next; i++; } node *second = last->next; last->next = NULL; return second; } int main () //main function { int k; node *head = NULL; int n = buildList (head); cout << "Linked list data: "; display (head); cout << "\nEnter the value of 'n': "; cin >> k; node *temp = append (head, k, n); cout << "\nAfter appending the last n nodes of a linked list to the beginning of the list\n"; cout << "Linked list data: "; display (temp); // printing last n nodes display (head); //printing remaining nodes }
Output: Enter the size of list:5 Enter data of the nodes 12 25 36 47 58 Linked list data: 12 25 36 47 58 Enter the value of 'n': 3 After appending the last n nodes of a linked list to the beginning of the list Linked list data: 36 47 58 12 25
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Singly Linked List
- Introduction to Linked List in Data Structure
Click Here - Linked List in –
- Singly Linked List in –
- Insertion in singly Linked List –
- Insertion at beginning in singly Linked List –
- Insertion at nth position in singly Linked List –
- Insertion at end in singly Linked List –
- Deletion in singly Linked List –
- Deletion from beginning in singly linked list :
- Deletion from nth position in singly linked list :
- Deletion from end in singly linked list :
- Linked List Insertion and Deletion –
C | C++ | Java - Reverse a linked list without changing links between nodes (Data reverse only) –
C | C++ | Java - Reverse a linked list by changing links between nodes –
- Print reverse of a linked list without actually reversing –
- Print reverse of a linked list without actually reversing –
- Insertion in the middle Singly Linked List –
- Insertion in a Sorted Linked List –
- Delete alternate nodes of a Linked List –
- Find middle of the linked list –
- Reverse a linked list in groups of given size –
- Find kth node from end of the linked list –
- Append the last n nodes of a linked list to the beginning of the list –
- Check whether linked list is palindrome or not –
- Fold a Linked List –
- Insert at given Position –
- Deletion at given Position –
Singly Linked List
- Introduction to Linked List in Data Structure
- Linked List in – C | C++ | Java
- Singly Linked List in – C | C++ | Java
- Insertion in singly Linked List – C | C++ | Java
- Deletion in singly Linked List – C | C++ | Java
- Reverse a linked list without changing links between nodes (Data reverse only) – C | C++ | Java
- Linked List Insertion and Deletion – C | C++ | Java
- Reverse a linked list by changing links between nodes – C | C++ | Java
- Linked List insertion in the middle – C | C++ | Java
- Print reverse of a linked list without actually reversing – C |C++ | Java
- Search an element in a linked list – C | C++ | Java
- Insertion in a Sorted Linked List – C | C++ | Java
- Delete alternate nodes of a Linked List – C | C++ | Java
- Find middle of the linked list – C | C++ | Java
- Reverse a linked list in groups of given size – C | C++ | Java
- Find kth node from end of the linked list – C | C++ | Java
- Append the last n nodes of a linked list to the beginning of the list – C | C++ | Java
- Check whether linked list is palindrome or not – C | C++ | Java
- Fold a Linked List – C | C++ | Java
- Insert at a given position – C | C++ | Java
- Delete at a given position – C | C++ | Java
Login/Signup to comment