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

206 Reverse Linked List

时间:2015-05-10 15:49:19      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:leetcode

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) {
        if(head == null){
            return head;
        }
        ListNode p = head.next;
        head.next = null;
        ListNode q = null;
        while(p != null){
            q = p.next;
            p.next = head;
            head = p;
            p = q;
        }
        return head;
    }
}

206 Reverse Linked List

标签:leetcode

原文地址:http://blog.csdn.net/havedream_one/article/details/45620773

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