Be the first user to complete this post

  • 0
Add to List
Medium

107. Alternate Splitting of a given Linked List

Objective: Split a linked list 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 the head and next to fix the new linked list headers.
  • 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.
 

Output:

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