Alternate Splitting of a given Linked List

Objective: Given a singly linked list, split it into two linked lists. These linked lists will contain the alternate nodes from the given linked list.

Example:

Alternate Splitting of a given Linked List

Approach:

  • Make two pointers(headA and headB) and set it to head and next just to fix the headers for the new linked list.
  • Make Node currNode = head and Node t = currNode.next.
  • Do the traversal of the linked list.
  • Make next = t.next and t.next = currNode.next.next. (Splitting step).
  • Keep checking whether next!=null && currNode.next.next!=null.

Complete Code:


Output:

->1->2->1->2->1->2
->1->1->1
->2->2->2

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

%d bloggers like this: