Java Program For Alternating Split Of A Given Singly Linked List- Set 1 Last Updated : 23 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Write a function AlternatingSplit() that takes one list and divides up its nodes to make two smaller lists 'a' and 'b'. The sublists should be made from alternating elements in the original list. So if the original list is 0->1->0->1->0->1 then one sublist should be 0->0->0 and the other should be 1->1->1. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Method (Using Dummy Nodes): Here is an approach that builds the sub-lists in the same order as the source list. The code uses temporary dummy header nodes for the 'a' and 'b' lists as they are being built. Each sublist has a "tail" pointer that points to its current last node — that way new nodes can be appended to the end of each list easily. The dummy nodes give the tail pointers something to point to initially. The dummy nodes are efficient in this case because they are temporary and allocated in the stack. Alternately, local "reference pointers" (which always point to the last pointer in the list instead of to the last node) could be used to avoid Dummy nodes. Java // Java program to implement // the above approach static void AlternatingSplit(Node source, Node aRef, Node bRef) { Node aDummy = new Node(); // Points to the last node in 'a' Node aTail = aDummy; Node bDummy = new Node(); // Points to the last node in 'b' Node bTail = bDummy; Node current = source; aDummy.next = null; bDummy.next = null; while (current != null) { // Add at 'a' tail MoveNode((aTail.next), current); // Advance the 'a' tail aTail = aTail.next; if (current != null) { MoveNode((bTail.next), current); bTail = bTail.next; } } aRef = aDummy.next; bRef = bDummy.next; } // This code is contributed by rutvik_56 Time Complexity: O(n) where n is number of node in the given linked list. Space Complexity: O(1),No extra space is used other than the three references to the nodes.Source: http://cslibrary.stanford.edu/105/LinkedListProblems.pdf Please refer complete article on Alternating split of a given Singly Linked List | Set 1 for more details! Comment More infoAdvertise with us Next Article Java Program For Alternating Split Of A Given Singly Linked List- Set 1 K kartik Follow Improve Article Tags : Java Linked Lists Practice Tags : Java Similar Reads Javascript Program For Alternating Split Of A Given Singly Linked List- Set 1 Write a function AlternatingSplit() that takes one list and divides up its nodes to make two smaller lists 'a' and 'b'. The sublists should be made from alternating elements in the original list. So if the original list is 0->1->0->1->0->1 then one sublist should be 0->0->0 and 3 min read Javascript Program For Reversing A Linked List In Groups Of Given Size - Set 1 Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Example: Input: 1->2->3->4->5->6->7->8->NULL, K = 3 Output: 3->2->1->6->5->4->8->7->NULL Input: 1->2->3->4->5->6->7->8->NULL, 3 min read Javascript Program For Reversing A Linked List In Groups Of Given Size- Set 2 Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Examples:Input: 1->2->3->4->5->6->7->8->NULL and k = 3 Output: 3->2->1->6->5->4->8->7->NULL. Input: 1->2->3->4->5->6->7->8->NU 3 min read Javascript Program For Arranging Single Linked List In Alternate Odd and Even Nodes Order Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where 7 min read Reverse the order of all nodes at even position in given Linked List Given a linked list A[] of N integers, the task is to reverse the order of all integers at an even position. Examples: Input: A[] = 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULLOutput: 1 6 3 4 5 2Explanation: Nodes at even position in the given linked list are 2, 4 and 6. So, after reversing 10 min read Like