aboutsummaryrefslogtreecommitdiff
path: root/odd_even_linke_list.cpp
blob: 4ee4bac929b6cc0d95305e0071933411cc540e8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "leetcode.h"

class Solution {
public:
  ListNode *oddEvenList(ListNode *head) {
    ListNode *odd = NULL, *headOdd = NULL, *even = NULL, *headEven = NULL;
    if (!head || !head->next || !head->next->next)
      return head;
    odd = head;
    headOdd = odd;
    even = head->next;
    headEven = even;
    while (even && even->next) {
      odd->next = odd->next->next;
      even->next = even->next->next;
      odd = odd->next;
      even = even->next;
    }
    odd->next = headEven;
    return headOdd;
  }
};