码迷,mamicode.com
首页 > 其他好文 > 详细

[leedcode 206] Reverse Linked List

时间:2015-08-04 00:10:48      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

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

 

[leedcode 206] Reverse Linked List

标签:

原文地址:http://www.cnblogs.com/qiaomu/p/4700656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!