zoukankan      html  css  js  c++  java
  • 在时间复杂度为O(n)且空间复杂度为O(1)的情况下翻转链表

    class NodeList{
        private Node head;
        private Node tail;
    
        void print(){
            Node node = head;
            while (node != null){
                System.out.print(node.date+",");
                node = node.next;
            }
            System.out.println();
        }
        private void add(int val){
            Node newNode = new Node(val);
            if(head == null){
                head = newNode;
                tail = newNode;
            }else {
                tail.next = newNode;
                tail = newNode;
            }
        }
    
        private void rever(){
            tail = head;
            Node pre = null;
            Node temp = null;
            Node p = head;
            while (p != null){
                temp = p.next;
                p.next = pre;
                pre = p;
                p = temp;
            }
            head = pre;
        }
    
        public static void main(String[] args) {
            NodeList list = new NodeList();
            list.add(1);
            list.add(2);
            list.add(3);
    
            list.rever();
            list.print();
        }
    }
    
    class Node{
        public int date;
        public Node next;
    
    
    
        public Node(int date) {
            this.date = date;
        }
    }
  • 相关阅读:
    CSS3美化网页元素
    表单
    列表,表格与媒体元素
    HTML5基础
    双列集合map-1
    单列集合List
    kafka-Eagle的安装
    kafka-自定义拦截器(interceptor)
    kafka-Consumer API
    kafka-Producer API
  • 原文地址:https://www.cnblogs.com/dongma/p/13169041.html
Copyright © 2011-2022 走看看