Be the first user to complete this post

  • 0
Add to List
Medium

107. 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.

Code:


Output:

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