zoukankan      html  css  js  c++  java
  • [leedcode 206] Reverse Linked List

    Reverse a singly linked list.

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode reverseList(ListNode head) {
            //需要画图,注意每个节点的起始位置,注意第一个rear为null
            if(head==null||head.next==null) return head;
            ListNode rear=null;
            ListNode p=head;
            ListNode pre=head;
            while(pre!=null){
                pre=pre.next;
                p.next=rear;
                rear=p;
                p=pre;
            }
            return rear;
            
        }
    }
  • 相关阅读:
    数据库的......
    数据库
    XML
    网络编程
    I/O系统---流
    周结

    集合,框架
    Spring入门
    Java Wed
  • 原文地址:https://www.cnblogs.com/qiaomu/p/4700656.html
Copyright © 2011-2022 走看看