Wednesday, October 8, 2025
HomeData Modelling & AIJava Program For Alternating Split Of A Given Singly Linked List- Set...

Java 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 the other should be 1->1->1.

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!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
23 Jan, 2023
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Dominic
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11875 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6832 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS