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
|
class Solution { public void reorderList(ListNode head) { ListNode fast = head, slow = head; while (fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; } ListNode pre = null, cur = slow.next; slow.next = null; while (cur != null) { ListNode temp = cur.next; cur.next = pre; pre = cur; cur = temp; } cur = head; while (pre != null) { ListNode temp = pre.next; pre.next = cur.next; cur.next = pre; cur = pre.next; pre = temp; } } }
|