Problem:
Reverse a singly linked list.Java Code:(难得做出个一次AC的)
/**
* 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) {
ListNode pre = null;
ListNode p = head;
while (p != null) {
ListNode tmp = p.next;
p.next = pre;
pre = p;
p = tmp;
}
return pre;
}
}
没有评论:
发表评论