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概述
- 下一篇: 关于在ubuntu下配置AMD显卡驱动的