aboutsummaryrefslogtreecommitdiff
path: root/reorder_list.c
blob: c44a84651580e439a46988c4b20473849171b1c6 (plain)
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
// 143. Reorder List
#include "leetcode.h"

/*
 * given the head of a singly linked list. the list can be represented as 'l0 ->
 * l1 -> ... -> Ln'. reorder the list to be of the following form: 'l0 -> ln ->
 * ln-1 -> ln-2'. you may not modify the values in the list's nodes. only nodes
 * themselves may be changed.
 */

struct ListNode {
  int val;
  struct ListNode *next;
};

struct ListNode *get_mid(struct ListNode *head) {
  struct ListNode *fast = head, *slow = head;
  while (fast->next && fast->next->next) {
    fast = fast->next->next;
    slow = slow->next;
  }
  return slow;
}

struct ListNode *reverse(struct ListNode *head) {
  struct ListNode *tail = NULL;
  while (head) {
    struct ListNode *curr = head;
    head = head->next;
    curr->next = tail;
    tail = curr;
  }
  return tail;
}

void merge(struct ListNode *n, struct ListNode *m) {
  struct ListNode *head = n;
  while (m) {
    struct ListNode *tmp = m->next;
    m->next = head->next;
    head->next = m;
    head = head->next->next;
    m = tmp;
  }
}

void reorderList(struct ListNode *head) {
  if (!head || !head->next || !head->next->next)
    return;
  struct ListNode *mid = get_mid(head), *half = mid->next;
  mid->next = NULL;
  half = reverse(half);
  merge(head, half);
}