143. Reorder List
生活随笔
收集整理的這篇文章主要介紹了
143. Reorder List
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/** 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;// 這一步很重要,因?yàn)槿绻鹟2的下一個(gè)不能指向nulll2.next = tem == null ? ne : tem;l1 = tem;l2 = ne;}}
?
轉(zhuǎn)載于:https://www.cnblogs.com/zmyvszk/p/5529832.html
總結(jié)
以上是生活随笔為你收集整理的143. Reorder List的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 边工作边刷题:70天一遍leetcode
- 下一篇: [翻译] FastReport Clas