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.
- 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.
->1->2->1->2->1->2 ->1->1->1 ->2->2->2
Top Companies Interview Questions..-
If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.