KatInterview

328. Odd Even Linked List

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to reorder a singly linked list by grouping all nodes with odd indices together, followed by all nodes with even indices. The first node is considered odd (index 1), the second even (index 2), and so on. The relative order within both the odd and even groups must be preserved. We are required to solve this in O(1) extra space and O(n) time.

Solution

Odd Even Linked List in Python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        # Handle the case of an empty list.
        if not head:
            return None

        # 'odd' pointer starts at the first node.
        odd = head
        # 'even' pointer starts at the second node.
        even = head.next
        # 'even_head' stores the head of the even-indexed list.
        even_head = even

        # Iterate as long as we have a pair of nodes (even and its next) to process.
        while even and even.next:
            # Link the current odd node to the next odd node (which is even.next).
            odd.next = even.next
            # Move the odd pointer forward to this new node.
            odd = odd.next

            # Link the current even node to the next even node (which is odd.next).
            even.next = odd.next
            # Move the even pointer forward.
            even = even.next

        # After the loop, connect the tail of the odd-indexed list 
        # to the head of the even-indexed list.
        odd.next = even_head

        return head

Odd Even Linked List in Java

/**
 * 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) {
        // Handle the case of an empty list.
        if (head == null) {
            return null;
        }

        // 'odd' pointer starts at the first node.
        ListNode odd = head;
        // 'even' pointer starts at the second node.
        ListNode even = head.next;
        // 'evenHead' stores the head of the even-indexed list.
        ListNode evenHead = even;

        // Iterate as long as we have a pair of nodes (even and its next) to process.
        while (even != null && even.next != null) {
            // Link the current odd node to the next odd node (which is even.next).
            odd.next = even.next;
            // Move the odd pointer forward to this new node.
            odd = odd.next;

            // Link the current even node to the next even node (which is odd.next).
            even.next = odd.next;
            // Move the even pointer forward.
            even = even.next;
        }

        // After the loop, connect the tail of the odd-indexed list 
        // to the head of the even-indexed list.
        odd.next = evenHead;

        return head;
    }
}

Odd Even Linked List in C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {
        // Handle the case of an empty list.
        if (!head) {
            return nullptr;
        }

        // 'odd' pointer starts at the first node.
        ListNode* odd = head;
        // 'even' pointer starts at the second node.
        ListNode* even = head->next;
        // 'evenHead' stores the head of the even-indexed list.
        ListNode* evenHead = even;

        // Iterate as long as we have a pair of nodes (even and its next) to process.
        while (even && even->next) {
            // Link the current odd node to the next odd node (which is even->next).
            odd->next = even->next;
            // Move the odd pointer forward to this new node.
            odd = odd->next;

            // Link the current even node to the next even node (which is odd->next).
            even->next = odd->next;
            // Move the even pointer forward.
            even = even->next;
        }

        // After the loop, connect the tail of the odd-indexed list 
        // to the head of the even-indexed list.
        odd->next = evenHead;

        return head;
    }
};

Odd Even Linked List in TypeScript

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function oddEvenList(head: ListNode | null): ListNode | null {
    // Handle the case of an empty list.
    if (!head) {
        return null;
    }

    // 'odd' pointer starts at the first node.
    let odd = head;
    // 'even' pointer starts at the second node.
    let even = head.next;
    // 'evenHead' stores the head of the even-indexed list.
    const evenHead = even;

    // Iterate as long as we have a pair of nodes (even and its next) to process.
    while (even && even.next) {
        // Link the current odd node to the next odd node (which is even.next).
        odd.next = even.next;
        // Move the odd pointer forward to this new node.
        odd = odd.next;

        // Link the current even node to the next even node (which is odd.next).
        even.next = odd.next;
        // Move the even pointer forward.
        even = even.next;
    }

    // After the loop, connect the tail of the odd-indexed list 
    // to the head of the even-indexed list.
    odd.next = evenHead;

    return head;
}

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.