zoukankan      html  css  js  c++  java
  • Reverse linked list

    Reverse a linked list.

    Example

    For linked list 1->2->3, the reversed linked list is 3->2->1

    分析


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    /**
     * Definition for ListNode.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int val) {
     *         this.val = val;
     *         this.next = null;
     *     }
     * }
     */ 
    public class Solution {
        /**
         * @param head: The head of linked list.
         * @return: The new head of reversed linked list.
         */
        public ListNode reverse(ListNode head) {
            // write your code here
            if(head == nullreturn null;
             
            ListNode last = null;
            while(head.next != null){
                ListNode tmp = head.next;
                head.next = last;
                last = head;
                head = tmp;
            }
            head.next = last;
            return head;
        }
    }




  • 相关阅读:
    软件策划书
    对开发团队的看法
    对敏捷开发的认识
    企业单位
    Pg数据库的基础安装
    Windows Server 任务计划执行.exe
    2020.04.08 重新开始
    20200211 Oracle监听启动异常
    20191225 医疗行业数据仓库
    20191224 多维数据库
  • 原文地址:https://www.cnblogs.com/zhxshseu/p/302814bbd3a5b707b5d366ba194ec6ee.html
Copyright © 2011-2022 走看看