int32bit / leetcode

Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
in.txt
solve.cpp

Reorder List

Given a singly linked list L: `L0→L1→…→Ln-1→Ln`, reorder it to: `L0→Ln→L1→Ln-1→L2→Ln-2→…`

You must do this in-place without altering the nodes' values.

For example, Given {1,2,3,4}, reorder it to {1,4,2,3}.

Solution

```ListNode *getMidNode(ListNode *head) {
while (fast != nullptr) {
slow = slow->next;
fast = fast->next;
if (fast)
fast = fast->next;
}
return slow;
}```

```ListNode *reverse(ListNode *head) {
ListNode *prev = nullptr;
while (p) {
ListNode *q = p->next;
p->next = prev;
prev = p;
p = q;
}
return prev;
}```

```void merge(ListNode *left, ListNode *right) {
ListNode *p = left, *q = right;
while (p && q) {
ListNode *nextLeft = p->next;
ListNode *nextRight = q->next;
p->next = q;
q->next = nextLeft;
p = nextLeft;
q = nextRight;
}
if (p)
p->next = nullptr;
}```

```void reorderList(ListNode *head) {