Delete every kth Node from a circular linked list until only one node is left. Also print the intermediate lists.
Examples:
Input : n=4, k=2, list = 1->2->3->4 Output : 1->2->3->4->1 1->2->4->1 2->4->2 2->2 Input : n=9, k=4, list = 1->2->3->4->5->6->7->8->9 Output : 1->2->3->4->5->6->7->8->9->1 1->2->3->4->6->7->8->9->1 1->2->3->4->6->7->8->1 1->2->3->6->7->8->1 2->3->6->7->8->2 2->3->6->8->2 2->3->8->2 2->3->2 2->2
Algorithm
Repeat the following steps until there is only one node left in the list.
Case 1: List is empty.
If the list is empty, simply return.
Case 2: List has only one node.
If the list has only one node left, we will print the list and return as our goal is reached.
Case 3: List has more than one nodes.
Define two pointers curr and prev and initialize the pointer curr with the head node.
Traverse the list using curr pointer by iterating it k times.
- Node to be deleted is the first node of the list.
Condition to check this( curr == head && curr->next == head).
If yes, then move prev until it reaches the last node. After prev reaches the last node, set head = head -> next and prev -> next = head. Delete curr. - Node to be deleted is the last node in the list.
Condition to check this is (curr -> next == head).
If curr is the last node. Set prev -> next = head and delete the node curr by free(curr). - Node to be deleted is neither the first node nor the last node, then set prev -> next = temp -> next and delete curr.
// C++ program to delete every kth Node from // circular linked list. #include <bits/stdc++.h> using namespace std; /* structure for a Node */struct Node { int data; Node* next; Node(int x) { data = x; next = NULL; } }; /*Utility function to print the circular linked list*/void printList(Node* head) { if (head == NULL) return; Node* temp = head; do { cout << temp->data << "->"; temp = temp->next; } while (temp != head); cout << head->data << endl; } /*Function to delete every kth Node*/void deleteK(Node** head_ref, int k) { Node* head = *head_ref; // If list is empty, simply return. if (head == NULL) return; // take two pointers - current and previous Node *curr = head, *prev; while (true) { // Check if Node is the only Node\ // If yes, we reached the goal, therefore // return. if (curr->next == head && curr == head) break; // Print intermediate list. printList(head); // If more than one Node present in the list, // Make previous pointer point to current // Iterate current pointer k times, // i.e. current Node is to be deleted. for (int i = 0; i < k; i++) { prev = curr; curr = curr->next; } // If Node to be deleted is head if (curr == head) { prev = head; while (prev->next != head) prev = prev->next; head = curr->next; prev->next = head; *head_ref = head; free(curr); } // If Node to be deleted is last Node. else if (curr->next == head) { prev->next = head; free(curr); } else { prev->next = curr->next; free(curr); } } } /* Function to insert a Node at the end of a Circular linked list */void insertNode(Node** head_ref, int x) { // Create a new Node Node* head = *head_ref; Node* temp = new Node(x); // if the list is empty, make the new Node head // Also, it will point to itself. if (head == NULL) { temp->next = temp; *head_ref = temp; } // traverse the list to reach the last Node // and insert the Node else { Node* temp1 = head; while (temp1->next != head) temp1 = temp1->next; temp1->next = temp; temp->next = head; } } /* Driver program to test above functions */int main() { // insert Nodes in the circular linked list struct Node* head = NULL; insertNode(&head;, 1); insertNode(&head;, 2); insertNode(&head;, 3); insertNode(&head;, 4); insertNode(&head;, 5); insertNode(&head;, 6); insertNode(&head;, 7); insertNode(&head;, 8); insertNode(&head;, 9); int k = 4; // Delete every kth Node from the // circular linked list. deleteK(&head;, k); return 0; } |
1->2->3->4->5->6->7->8->9->1 1->2->3->4->6->7->8->9->1 1->2->3->4->6->7->8->1 1->2->3->6->7->8->1 2->3->6->7->8->2 2->3->6->8->2 2->3->8->2 2->3->2 2->2
Recommended Posts:
- Delete all the even nodes of a Circular Linked List
- Delete all odd or even positioned nodes from Circular Linked List
- Delete a given node in Linked List under given constraints
- Delete a node in a Doubly Linked List
- Delete a Linked List node at a given position
- Delete all Prime Nodes from a Circular Singly Linked List
- Delete a Doubly Linked List node at a given position
- Delete a Node from linked list without head pointer
- Recursive function to delete k-th node from linked list
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Convert singly linked list into circular linked list
- Swap Kth node from beginning with Kth node from end in a Linked List
- Check if a linked list is Circular Linked List
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.



