Reverse a singly linked list.
Solution:
/**
* 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 cur=head, reverse=null;
while(cur!=null){
ListNode tmp = cur.next;
cur.next = reverse;
reverse = cur;
cur = tmp;
}
return reverse;
}
}