首页 > 其他 > 详细

Reverse a Singly LinkedList

时间:2015-03-11 07:03:25      阅读:243      评论:0      收藏:0      [点我收藏+]
Reverse a Singly LinkedList
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */

这是面试的时候被问到的一题,还是考察LinkedList的理解,最后如何得到reverse之后的head的。

 

public Class Solution{

    public static ListNode reverseList(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        
        ListNode next = head.next;
        head.next = null;
        while(next != null){
            ListNode temp = next.next;
            next.next = head;
            head = next;
            next = temp;
        }
        
        return head;
    }
}

 

Reverse a Singly LinkedList

原文:http://www.cnblogs.com/incrediblechangshuo/p/4328898.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!