zoukankan      html  css  js  c++  java
  • 单链表应用(1)--使用快慢指针,找链表中间值

       //单链表的使用:快慢指针,如何找中间值
        public static void main(String[] args) {
            Node<Integer> node1 = new Node(1,null);
            Node<Integer> node2 = new Node(2,null);
            Node<Integer> node3 = new Node(3,null);
            Node<Integer> node4 = new Node(4,null);
            Node<Integer> node5 = new Node(5,null);
            Node<Integer> node6 = new Node(5,null);
            node1.next = node2;
            node2.next = node3;
            node3.next = node4;
            node4.next = node5;
            node5.next = node6;
    
            Node quick = node1;
            Node low = node1;
            while (quick != null && quick.next != null){
                quick = quick.next.next;
                low = low.next;
            }
            System.out.println("中间值:" + low.t);
        }
    
        private static class Node<T>{
            private T t;
            private Node next;
    
            public Node(T t, Node next) {
                this.t = t;
                this.next = next;
            }
        }
  • 相关阅读:
    示例 json with js
    JS json
    jquery
    发布包规范
    20180320待编辑
    CefSharp中c#和js交互
    列举mvc ActionResult的返回值
    怎么实现第三方登录
    mvc @Html控件
    MVC webuploader 图片
  • 原文地址:https://www.cnblogs.com/maohuidong/p/14216285.html
Copyright © 2011-2022 走看看