zoukankan      html  css  js  c++  java
  • 恢复博客更新 --- 逆序单项链表 要求空间复杂度o(1) 时间复杂度o(n)

    转眼已经2016年3月1日了,前一阵子忙了很多事,包括下决心换了工作,也算是正式进了互联网行业。现在后头想想,人必须趁着有时间有精力时多折腾,毕竟人生短短几十年,如果不折腾以后肯定会后悔。

    今天开始恢复更新博客了,计划今年要做的事:

    1. 复习微积分

    2. 复习概率论和统计学

    3. 刷一遍leetcode

    作为热身,先来一道最近微博上挺火的一道笔试题吧,据说没几个能无bug的写出来,我自己看了一下,觉得很简单,但写了一下还是不小心给绕进去了,看来算法题就得没事多练,既能预防老年痴呆,还能顺手提高智商。。。

    逆序单项链表 要求空间复杂度o(1) 时间复杂度o(n)

     1 package com.autonavi.algo;
     2 
     3 public class ReverseLinkedList {
     4     
     5     public static void main(String[] args){
     6         Node node0 = new Node(null,0);
     7         Node node1 = new Node(null,1);
     8         Node node2 = new Node(null,2);
     9         
    10         node0.next = node1;
    11         node1.next = node2;
    12         
    13         Node node = node0;
    14         while(null != node){
    15             System.out.println(node.value);
    16             node = node.next;
    17         }
    18         
    19         Node newHead = reverse(node0);
    20         
    21         node = newHead;
    22         while(null != node){
    23             System.out.println(node.value);
    24             node = node.next;
    25         }
    26     }
    27     
    28     public static Node reverse(Node head){
    29         Node p;
    30         Node tmp = head.next;
    31         head.next = null;
    32         while(tmp != null){
    33             p = tmp;
    34             tmp = tmp.next;
    35             p.next = head;
    36             head = p;
    37         }
    38         return head;
    39     }
    40 }
    41 
    42 class Node{
    43     Node next;
    44     int value;
    45     Node(Node next, int value){
    46         this.next = next;
    47         this.value = value;
    48     }
    49     @Override
    50     public String toString() {
    51         return "Node [next=" + next + ", value=" + value + "]";
    52     }
    53     
    54 }
  • 相关阅读:
    线程安全
    Kafka分区原理图
    Zookeeper02
    Zookeeper01
    kafka01
    20170623_oracle_SQL
    20170623_oracle备份和恢复_常见问题
    20170623_oracle基础知识_常见问题
    数字类型入门
    数据类型基础
  • 原文地址:https://www.cnblogs.com/aalex/p/5231492.html
Copyright © 2011-2022 走看看