← Home

328. Odd Even Linked List

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
328

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 328. Odd Even Linked List, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are oddEvenList.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(N), space: O(1)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 20 ms, faster than 58.58% of C++ online submissions for Odd Even Linked List.
02//Memory Usage: 10.1 MB, less than 8.57% of C++ online submissions for Odd Even Linked List.
03
04/**
05 * Definition for singly-linked list.
06 * struct ListNode {
07 *     int val;
08 *     ListNode *next;
09 *     ListNode(int x) : val(x), next(NULL) {}
10 * };
11 */
12class Solution {
13public:
14    ListNode* oddEvenList(ListNode* head) {
15        ListNode *oddHead = new ListNode(0); //virtual head
16        ListNode *odd = oddHead;
17        ListNode *evenHead = new ListNode(0); //virtual head
18        ListNode *even = evenHead;
19        ListNode *cur = head;
20        int i = 1;
21        
22        while(cur){
23            if(i % 2 == 1){
24                odd->next = cur;
25                odd = odd->next;
26            }else{
27                even->next = new ListNode(cur->val);
28                even = even->next;
29            }
30            i++;
31            cur = cur->next;
32        }
33        
34        odd->next = evenHead->next;
35        return oddHead->next;
36    }
37};
38
39//Runtime: 12 ms, faster than 99.78% of C++ online submissions for Odd Even Linked List.
40//Memory Usage: 10.3 MB, less than 8.57% of C++ online submissions for Odd Even Linked List.
41//time: O(N), space: O(1)
42/**
43 * Definition for singly-linked list.
44 * struct ListNode {
45 *     int val;
46 *     ListNode *next;
47 *     ListNode(int x) : val(x), next(NULL) {}
48 * };
49 */
50class Solution {
51public:
52    ListNode* oddEvenList(ListNode* head) {
53        if(!head) return NULL;
54        ListNode *odd = head, *even = head->next, *evenHead = even;
55        while(even && even->next){
56            //this loop stops when even->next is meaningless
57            odd->next = even->next;
58            odd = odd->next;
59            even->next = odd->next;
60            even = even->next;
61        }
62        odd->next = evenHead;
63        return head;
64    }
65};

Cost

Complexity

Time
O(N), space: O(1)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.