shlogg · Early preview
Prashant Mishra @prashantrmishra

Merging Odd And Even Linked List Nodes Efficiently

Reorder linked list by alternating between odd and even nodes. Create two lists, one for odd and one for even values. Connect last node of odd list to head of even list.

Problem
We have to keep index i tracking the node values if i is odd then put them in a different say odd, else put them in a list say even.
At the end connect the last node of the odd list to head of even list

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode oddEvenList(ListNode head) {
        ListNode odd  = new ListNode(0);
        ListNode even...