143. Reorder List - Medium
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You may not modify the values in the list's nodes, only nodes itself may be changed.
Example 1:
Given 1->2->3->4, reorder it to 1->4->2->3.
Example 2:
Given 1->2->3->4->5, reorder it to 1->5->2->4->3.
first use two pointers (slow, fast) to find the middle node of the linked list;
then reverse the second half of the linked list;
then merge the two halves one by one.
p.s. 注意合并两个linked list时候的处理
time: O(n), space: O(1)
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public void reorderList(ListNode head) { if(head == null || head.next == null) { return; } // find middle node ListNode fast = head, slow = head; while(fast.next != null && fast.next.next != null) { fast = fast.next.next; slow = slow.next; } // split into two halves, reverse the second half ListNode second = reverse(slow.next); slow.next = null; // merge merge(head, second); } private ListNode reverse(ListNode head) { if(head == null || head.next == null) { return head; } ListNode prev = null, cur = head; while(cur != null) { ListNode next = cur.next; cur.next = prev; prev = cur; cur = next; } return prev; } private void merge(ListNode p1, ListNode p2) { while(p1 != null && p2 != null) { ListNode n1 = p1.next; ListNode n2 = p2.next; p1.next = p2; p2.next = n1; p1 = n1; p2 = n2; } } }