Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Alternate Splitting of a given Linked List

Objec­tive: Given a singly linked list, split it into two linked lists. These linked lists will con­tain the alter­nate nodes from the given linked list.

Exam­ple:

Alternate-Splitting-of-a-given-Linked-List

Alternate-Splitting-of-a-given-Linked-List

Approach:

  • Make two point­ers(headA and headB) and set it to head and next just to fix the head­ers for the new linked list.
  • Make Node cur­rN­ode = head and Node t = currNode.next.
  • Do the tra­ver­sal of the linked list.
  • Make next = t.next and t.next = currNode.next.next. (Split­ting step).
  • Keep check­ing whether next!=null && currNode.next.next!=null.

Com­plete Code:


Out­put:

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

You may also like...