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) {
ListNode h = new ListNode(0);
while(head!=null){
ListNode n = head.next;
head.next = h.next;
h.next = head;
head = n;
}
return h.next;
}
}