reorder-list
生活随笔
收集整理的这篇文章主要介绍了
reorder-list
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ //思路:通过不同的首结点获取到不同的尾结点,然后拼接 public class Solution { public ListNode getLastNode(ListNode head) { ListNode first = head; ListNode fakeLast = head; if (head.next == null) { return head; } while (first.next != null) { fakeLast = first; first = first.next; } ListNode last = fakeLast.next; fakeLast.next = null; return last; } public void reorderList(ListNode head) { if(head==null){ return; } // 真的首结点 ListNode first = head; // 假的首结点 ListNode fakeFirst = head; while (first.next != null) { ListNode last = getLastNode(first); fakeFirst = first.next; if (fakeFirst == null) { first.next = last; break; } else { first.next = last; last.next = fakeFirst; first = fakeFirst; } } } }
转载于:https://www.cnblogs.com/qingtianBKY/p/6896754.html
总结
以上是生活随笔为你收集整理的reorder-list的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 七、pipeLine概述
- 下一篇: Servlet 与 Ajax 交互一直报