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||head.next==null) return head;
ListNode A= head, B=A.next, C = B.next;
while(B!=null){
C=B.next;
B.next=A;
A=B;
B=C;
}
head.next = null;
return A;
}
}