void reorderList(ListNode* head) {
ListNode* cur = head;
while (cur && cur->next && cur->next->next) {
ListNode* prev = cur;
while (prev->next->next) prev = prev->next;
ListNode* last = prev->next;
prev->next = nullptr;
last->next = cur->next;
cur->next = last;
cur = last->next;
}
return;
}
public void reorderList(ListNode head) {
ListNode cur = head;
while (cur != null && cur.next != null && cur.next.next != null) {
ListNode prev = cur;
while (prev.next.next != null) prev = prev.next;
ListNode last = prev.next;
prev.next = null;
last.next = cur.next;
cur.next = last;
cur = last.next;
}
return;
}
def reorder_list(head):
cur = head
while cur and cur.next and cur.next.next:
prev = cur
while prev.next.next:
prev = prev.next
last = prev.next
prev.next = None
last.next = cur.next
cur.next = last
cur = last.next
return
function reorderList(head: ListNode | null): void {
let cur = head;
while (cur && cur.next && cur.next.next) {
let prev = cur;
while (prev.next!.next) prev = prev.next!;
const last = prev.next!;
prev.next = null;
last.next = cur.next;
cur.next = last;
cur = last.next;
}
return;
}
void reorderList(ListNode* head) {
if (!head) return;
vector<ListNode*> nodes;
for (ListNode* n = head; n; n = n->next) nodes.push_back(n);
int i = 0;
int j = (int)nodes.size() - 1;
while (i < j) {
nodes[i]->next = nodes[j];
i++;
if (i == j) break;
nodes[j]->next = nodes[i];
j--;
}
nodes[i]->next = nullptr;
return;
}
public void reorderList(ListNode head) {
if (head == null) return;
List<ListNode> nodes = new ArrayList<>();
for (ListNode n = head; n != null; n = n.next) nodes.add(n);
int i = 0;
int j = nodes.size() - 1;
while (i < j) {
nodes.get(i).next = nodes.get(j);
i++;
if (i == j) break;
nodes.get(j).next = nodes.get(i);
j--;
}
nodes.get(i).next = null;
return;
}
def reorder_list(head):
if not head:
return
nodes = []
n = head
while n:
nodes.append(n)
n = n.next
i = 0
j = len(nodes) - 1
while i < j:
nodes[i].next = nodes[j]
i += 1
if i == j:
break
nodes[j].next = nodes[i]
j -= 1
nodes[i].next = None
return
function reorderList(head: ListNode | null): void {
if (!head) return;
const nodes: ListNode[] = [];
for (let n: ListNode | null = head; n; n = n.next) nodes.push(n);
let i = 0;
let j = nodes.length - 1;
while (i < j) {
nodes[i].next = nodes[j];
i++;
if (i === j) break;
nodes[j].next = nodes[i];
j--;
}
nodes[i].next = null;
return;
}
void reorderList(ListNode* head) {
if (!head || !head->next) return;
ListNode* slow = head;
ListNode* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
ListNode* prev = nullptr;
ListNode* curr = slow->next;
slow->next = nullptr;
while (curr) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
ListNode* first = head;
ListNode* second = prev;
while (second) {
ListNode* t1 = first->next;
ListNode* t2 = second->next;
first->next = second;
second->next = t1;
first = t1;
second = t2;
}
return;
}
public void reorderList(ListNode head) {
if (head == null || head.next == null) return;
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode prev = null;
ListNode curr = slow.next;
slow.next = null;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
ListNode first = head;
ListNode second = prev;
while (second != null) {
ListNode t1 = first.next;
ListNode t2 = second.next;
first.next = second;
second.next = t1;
first = t1;
second = t2;
}
return;
}
def reorder_list(head):
if not head or not head.next:
return
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
prev = None
curr = slow.next
slow.next = None
while curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
first = head
second = prev
while second:
t1 = first.next
t2 = second.next
first.next = second
second.next = t1
first = t1
second = t2
return
function reorderList(head: ListNode | null): void {
if (!head || !head.next) return;
let slow: ListNode = head;
let fast: ListNode | null = head;
while (fast && fast.next) {
slow = slow.next!;
fast = fast.next.next;
}
let prev: ListNode | null = null;
let curr: ListNode | null = slow.next;
slow.next = null;
while (curr) {
const next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
let first: ListNode | null = head;
let second = prev;
while (second) {
const t1 = first!.next;
const t2 = second.next;
first!.next = second;
second.next = t1;
first = t1;
second = t2;
}
return;
}
void reorderList(ListNode* head) {
if (!head || !head->next || !head->next->next) return;
ListNode* slow = head;
ListNode* fast = head->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
ListNode* prev = nullptr;
ListNode* curr = slow->next;
slow->next = nullptr;
while (curr) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
ListNode* a = head;
ListNode* b = prev;
while (a && b) {
ListNode* aNext = a->next;
ListNode* bNext = b->next;
a->next = b;
b->next = aNext;
a = aNext;
b = bNext;
}
return;
}
public void reorderList(ListNode head) {
if (head == null || head.next == null || head.next.next == null) return;
ListNode slow = head;
ListNode fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode prev = null;
ListNode curr = slow.next;
slow.next = null;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
ListNode a = head;
ListNode b = prev;
while (a != null && b != null) {
ListNode aNext = a.next;
ListNode bNext = b.next;
a.next = b;
b.next = aNext;
a = aNext;
b = bNext;
}
return;
}
def reorder_list(head):
if not head or not head.next or not head.next.next:
return
slow = head
fast = head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
prev = None
curr = slow.next
slow.next = None
while curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
a = head
b = prev
while a and b:
a_next = a.next
b_next = b.next
a.next = b
b.next = a_next
a = a_next
b = b_next
return
function reorderList(head: ListNode | null): void {
if (!head || !head.next || !head.next.next) return;
let slow = head;
let fast = head.next;
while (fast && fast.next) {
slow = slow.next!;
fast = fast.next.next;
}
let prev: ListNode | null = null;
let curr: ListNode | null = slow.next;
slow.next = null;
while (curr) {
const next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
let a: ListNode | null = head;
let b = prev;
while (a && b) {
const aNext = a.next;
const bNext = b.next;
a.next = b;
b.next = aNext;
a = aNext;
b = bNext;
}
return;
}