Splitting starting N nodes into new Circular Linked List while preserving the old nodes
Given a circular linked list with N nodes and an integer K where 0 < K < N, the task is to split the first K nodes into a new list and at the same time preserving the rest of the nodes in the original circular linked list.
Examples:
Input: 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8, K = 3
Output:
Original list:
2 3 4 5 6 7 8
The new lists are:
2 3 4
5 6 7 8
Input: 2 -> 4 -> 6 -> 8- > 10 -> 12, N = 4
Output:
Original list:
2 4 6 8 10 12
The new lists are:
2 4 6 8
10 12
Approach:
- Traverse an iterator until the required node i.e. the Kth node.
- Point the node just previous to the Kth node to the head of the original list.
- Point the last node of the original list to the Kth node.
Below is the implementation of the above approach:
Java
// Java implementation of the approach public class CircularLinkedList { Node last; static class Node { int data; Node next; }; // Function to add a node to the empty list public Node addToEmpty(int data) { // If not empty if (this.last != null) return this.last; // Creating a node dynamically Node temp = new Node(); // Assigning the data temp.data = data; this.last = temp; // Creating the link this.last.next = this.last; return last; } // Function to add a node to the // beginning of the list public Node addBegin(int data) { // If list is empty if (last == null) return addToEmpty(data); // Create node Node temp = new Node(); // Assign data temp.data = data; temp.next = this.last.next; this.last.next = temp; return this.last; } // Function to traverse and print the list public void traverse() { Node p; // If list is empty if (this.last == null) { System.out.println("List is empty."); return; } // Pointing to the first Node of the list p = this.last.next; // Traversing the list do { System.out.print(p.data + " "); p = p.next; } while (p != this.last.next); System.out.println(""); } // Function to find the length of the CircularLinkedList public int length() { // Stores the length int x = 0; // List is empty if (this.last == null) return x; // Iterator Node to traverse the List Node itr = this.last.next; while (itr.next != this.last.next) { x++; itr = itr.next; } // Return the length of the list return (x + 1); } // Function to split the first k nodes into // a new CircularLinkedList and the remaining // nodes stay in the original CircularLinkedList public Node split(int k) { // Empty Node for reference Node pass = new Node(); // Check if the list is empty // If yes, then return null if (this.last == null) return this.last; // NewLast will contain the last node of // the new split list // itr to iterate the node till // the required node Node newLast, itr = this.last; for (int i = 0; i < k; i++) { itr = itr.next; } // Update NewLast to the required node and // link the last to the start of rest of the list newLast = itr; pass.next = itr.next; newLast.next = this.last.next; this.last.next = pass.next; // Return the last node of the required list return newLast; } // Driver code public static void main(String[] args) { CircularLinkedList clist = new CircularLinkedList(); clist.last = null; clist.addToEmpty(12); clist.addBegin(10); clist.addBegin(8); clist.addBegin(6); clist.addBegin(4); clist.addBegin(2); System.out.println("Original list:"); clist.traverse(); int k = 4; // Create a new list for the starting k nodes CircularLinkedList clist2 = new CircularLinkedList(); // Append the new last node into the new list clist2.last = clist.split(k); // Print the new lists System.out.println("The new lists are:"); clist2.traverse(); clist.traverse(); } } |
C#
// C# implementation of the approach using System; public class CircularLinkedList { public Node last; public class Node { public int data; public Node next; }; // Function to add a node to the empty list public Node addToEmpty(int data) { // If not empty if (this.last != null) return this.last; // Creating a node dynamically Node temp = new Node(); // Assigning the data temp.data = data; this.last = temp; // Creating the link this.last.next = this.last; return last; } // Function to add a node to the // beginning of the list public Node addBegin(int data) { // If list is empty if (last == null) return addToEmpty(data); // Create node Node temp = new Node(); // Assign data temp.data = data; temp.next = this.last.next; this.last.next = temp; return this.last; } // Function to traverse and print the list public void traverse() { Node p; // If list is empty if (this.last == null) { Console.WriteLine("List is empty."); return; } // Pointing to the first Node of the list p = this.last.next; // Traversing the list do { Console.Write(p.data + " "); p = p.next; } while (p != this.last.next); Console.WriteLine(""); } // Function to find the length of the CircularLinkedList public int length() { // Stores the length int x = 0; // List is empty if (this.last == null) return x; // Iterator Node to traverse the List Node itr = this.last.next; while (itr.next != this.last.next) { x++; itr = itr.next; } // Return the length of the list return (x + 1); } // Function to split the first k nodes into // a new CircularLinkedList and the remaining // nodes stay in the original CircularLinkedList public Node split(int k) { // Empty Node for reference Node pass = new Node(); // Check if the list is empty // If yes, then return null if (this.last == null) return this.last; // NewLast will contain the last node of // the new split list // itr to iterate the node till // the required node Node newLast, itr = this.last; for (int i = 0; i < k; i++) { itr = itr.next; } // Update NewLast to the required node and // link the last to the start of rest of the list newLast = itr; pass.next = itr.next; newLast.next = this.last.next; this.last.next = pass.next; // Return the last node of the required list return newLast; } // Driver code public static void Main(String[] args) { CircularLinkedList clist = new CircularLinkedList(); clist.last = null; clist.addToEmpty(12); clist.addBegin(10); clist.addBegin(8); clist.addBegin(6); clist.addBegin(4); clist.addBegin(2); Console.WriteLine("Original list:"); clist.traverse(); int k = 4; // Create a new list for the starting k nodes CircularLinkedList clist2 = new CircularLinkedList(); // Append the new last node into the new list clist2.last = clist.split(k); // Print the new lists Console.WriteLine("The new lists are:"); clist2.traverse(); clist.traverse(); } } // This code is contributed by Rajput-Ji |
Original list: 2 4 6 8 10 12 The new lists are: 2 4 6 8 10 12
Recommended Posts:
- Sum of the nodes of a Circular Linked List
- Delete all the even nodes of a Circular Linked List
- Count nodes in Circular linked list
- Exchange first and last nodes in Circular Linked List
- Delete all odd or even positioned nodes from Circular Linked List
- Delete all Prime Nodes from a Circular Singly Linked List
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Delete N nodes after M nodes of a linked list
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Linked List Sum of Nodes Between 0s
- Sum and Product of all the nodes which are less than K in the linked list
- Linked List Product of Nodes Between 0s
- Sum of all odd frequency nodes of the Linked List
- Find the sum of last n nodes of the given 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.
Improved By : Rajput-Ji



