[LeetCode]143. Reorder List
这个题是基本技能的一个集合,用到了快慢指针找中点,反转链表和合并链表
一些链表的拼接过程要熟练
public void reorderList(ListNode head) { /* 先找到中点,然后反转后部分,然后组合链表 */ if (head==null) return; //第一步,快慢指针找中点 ListNode slow = head; ListNode fast = head; while (fast!=null&&fast.next!=null) { slow = slow.next; fast = fast.next.next; } ListNode back = slow.next; slow.next = null; //反转后边 ListNode pre = null; while (back!=null) { ListNode next = back.next; back.next = pre; pre = back; back = next; } ListNode res = head; //拼接 while (res!=null&&pre!=null) { ListNode t1 = res.next; ListNode t2 = pre.next; res.next = pre; pre.next = t1; pre = t2; res = t1; } }