标签: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;
}
}
标签:leetcode
原文地址:http://blog.csdn.net/havedream_one/article/details/45620773