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;
                }
     
            }
        }
    }
  • 相关阅读:
    转:Jmeter之Bean shell使用(一)
    转:Curl详解
    转:MIME(Multipurpose Internet Mail Extensions)类型
    转:Tomcat配置
    转:windows 7系统安装与配置Tomcat服务器环境
    转:Apache POI Tutorial
    转:Busy Developers' Guide to HSSF and XSSF Features
    转:Java实现几种常见排序方法
    转:JAVA强制类型转换
    转:Java的各种类型转换汇总
  • 原文地址:https://www.cnblogs.com/qingtianBKY/p/6896754.html
Copyright © 2011-2022 走看看