Reorder List
一定要舍得画图,把指针的指向搞清楚。
public class Solution {
public void reorderList(ListNode head) {
if(head == null) return;
ListNode slow = head, fast = head;
while(fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode second = slow.next;
if(second == null) return;
slow.next = null;
ListNode prev = second, curr = second.next;
while(curr != null) {
ListNode temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
second.next = null;
second = prev;
ListNode first = head;
while(first != null && second != null) {
ListNode secondNext = second.next;
second.next = first.next;
first.next = second;
first = second.next;
second = secondNext;
}
}
}

浙公网安备 33010602011771号