/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode res = new ListNode(-1);
while(head != null) {
ListNode next = head.next;
head.next = res.next;
res.next = head;
head = next;
}
return res.next;
}
}