欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

143. Reorder List

发布时间:2025/3/15 编程问答 39 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。