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;
        }
    }




  • 相关阅读:
    2008年假期
    Asp.Net viewstate , session , cookie區別
    C#类型 参考表(MSDN)
    GIS ftp
    gis 好书推荐
    c#应该怎么改进?
    ArcEngine开发体验(附许可)
    gis开源开发资料(持续更新)
    GIS API乱弹
    Autodesk Map3d的应用和开发
  • 原文地址:https://www.cnblogs.com/zhxshseu/p/302814bbd3a5b707b5d366ba194ec6ee.html
Copyright © 2011-2022 走看看