zoukankan      html  css  js  c++  java
  • 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;
                }
     
            }
        }
    }
  • 相关阅读:
    R-CNN学习笔记
    Numpy和Pandas
    用python解决打标签时将xml文件的标签名打错
    爬虫Ⅱ:scrapy框架
    爬虫Ⅰ:爬虫的基础知识
    Linux学习笔记
    MySql笔记Ⅱ
    MySql笔记Ⅰ
    Qt数据库报错:“Unable to execute statement”
    Qt数据库报错“out of memory Error opening database“
  • 原文地址:https://www.cnblogs.com/qingtianBKY/p/6896754.html
Copyright © 2011-2022 走看看