单向链表的反转

遍历法

static Node reverse(Node head){
  if(head == null){
    return null;
  }
  Node pre = head;
  Node cur = head;
  Node reHead = null;

  //如果当前节点为空结束循环
  while(cur != null){
    //保存下一个节点以免丢失
    Node temp = cur.next;
    //1.对pre和cur结点的关系进行反转。本来是pre指向cur的,用下面这条代码能够把cur指向pre。
    cur.next = pre;
    //2.如果下一个结点为空,那他就是反转链表的头结点
    if(temp == null)
      rehead = cur;
    
    //上一个节点已经反转完成,现在要移动到下一个节点。
    pre = cur;
    cur = temp;

    return rehead;
    
  }
}


//简化版
static Node reverse(Node head){
  Node pre = null;
  Node cur = null;
  while(head != null){
    temp = head.next;
    head.next = pre;

    pre = head;
    head = temp;
  }
  return pre;
}

 

递归

static Node reverse(Node head){
  //我们从原链表的头结点开始
  Node cur = head;
  //递归到原链表的尾节点结束。递归到了尾节点的时候就返回当前节点
  if(cur == null || cur.next == null){
    return cur;
  }else{
    Node reHead = reverse(cur.next);
    cur.next.next = cur;
    cur.next = null;
    return reHead;
  }
}

内置法

LinkedList没有提供反转链表的相关函数,以下是通过foreach实现链表反转
static LinkedList reverse(LinkedList linkedList) {
    LinkedList<Object> newLinkedList = new LinkedList<>();
    for (Object object : linkedList) {
        newLinkedList.add(0, object);
    }
    return newLinkedList;
}

 

 

posted @ 2019-03-19 18:19  strawqqhat  阅读(170)  评论(0编辑  收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }