Bird
Raised Fist0
Interview Preplinked-list-advancedmediumAmazonGoogleFacebook

Odd Even Linked List

Choose your preparation mode3 modes available
</>
IDE
def oddEvenList(head: Optional[ListNode]) -> Optional[ListNode]:public ListNode oddEvenList(ListNode head)ListNode* oddEvenList(ListNode* head)function oddEvenList(head)
def oddEvenList(head):
    # Write your solution here
    pass
class Solution {
    public ListNode oddEvenList(ListNode head) {
        // Write your solution here
        return head;
    }
}
#include <vector>
using namespace std;

ListNode* oddEvenList(ListNode* head) {
    // Write your solution here
    return head;
}
function oddEvenList(head) {
    // Write your solution here
}
0/10
Common Bugs to Avoid
Wrong: [1, 2, 3, 4, 5]No rearrangement done; code returns input as is.Implement logic to separate odd and even nodes and link odd list to even list.
Wrong: [1, 3, 5, 4, 2]Even nodes reversed or order not preserved.Maintain even nodes in original order by linking them as encountered.
Wrong: [2, 1]Swapped odd and even nodes or indexing starts at 0 incorrectly.Index nodes starting at 1 and separate odd and even accordingly.
Wrong: [1, 2, 1, 2, 2, 1]Grouping by node value instead of position parity.Group nodes strictly by their position index parity, not by value.
Wrong: Timeout or no outputInefficient algorithm with O(n^2) or worse complexity.Use a single pass O(n) algorithm with two pointers and O(1) extra space.
Test Cases
t1_01basic
Input{"head":[1,2,3,4,5]}
Expected[1,3,5,2,4]

Nodes at odd positions 1,3,5 come first, followed by even positions 2,4.

t1_02basic
Input{"head":[2,1,3,5,6,4,7]}
Expected[2,3,7,1,5,4,6]

Odd indices nodes 2,3,7 followed by even indices nodes 1,5,4,6.

t2_01edge
Input{"head":[]}
Expected[]

Empty list input returns empty list.

t2_02edge
Input{"head":[10]}
Expected[10]

Single node list returns same list unchanged.

t2_03edge
Input{"head":[7,7,7,7]}
Expected[7,7,7,7]

All nodes have same value; order preserved with odd nodes first then even nodes.

t2_04edge
Input{"head":[1,2]}
Expected[1,2]

Two node list; odd node followed by even node.

t3_01corner
Input{"head":[1,2,3,4,5,6]}
Expected[1,3,5,2,4,6]

Even number of nodes; odd nodes 1,3,5 followed by even nodes 2,4,6.

t3_02corner
Input{"head":[1,2,3,4,5,6,7,8,9]}
Expected[1,3,5,7,9,2,4,6,8]

Odd nodes 1,3,5,7,9 followed by even nodes 2,4,6,8; tests off-by-one errors.

t3_03corner
Input{"head":[1,2,2,1,1,2]}
Expected[1,2,1,2,2,1]

Tests confusion between 0/1-based indexing and value-based grouping.

t4_01performance
Input{"head":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]}
⏱ Performance - must finish in 2000ms

n=100 nodes; O(n) time complexity required to complete within 2 seconds.