143. Reorder List
生活随笔
收集整理的这篇文章主要介绍了
143. Reorder List
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
/** 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的下一个不能指向nulll2.next = tem == null ? ne : tem;l1 = tem;l2 = ne;}}
转载于:https://www.cnblogs.com/zmyvszk/p/5529832.html
总结
以上是生活随笔为你收集整理的143. Reorder List的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 边工作边刷题:70天一遍leetcode
- 下一篇: [翻译] FastReport Clas