标签:
/* * 143. Reorder List * 11.28 by Mingyang 总体思想就是后半部分reverse然后再merge */ 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 tem = slow.next; slow.next = null; // 这里用了下面brink的代码 ListNode ne = reverseList(tem); mergeLists(head, ne); } public void mergeLists(ListNode l1, ListNode l2) { if (l1 == null && l2 == null) return; while (l1 != null && l2 != null) { ListNode tem = l1.next; ListNode ne = l2.next; l1.next = l2; // 这一步很重要,因为如果l2的下一个不能指向null l2.next = tem == null ? ne : tem; l1 = tem; l2 = ne; } }
标签:
原文地址:http://www.cnblogs.com/zmyvszk/p/5529832.html