class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public static ListNode reverse(ListNode head){
if (head == null || head.next == null) return head;
ListNode newHead = reverse(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
public static ListNode reverse2(ListNode head){
ListNode p1, p2, p3;
p1 = null;
p2 = head;
while(p2!=null){
p3 = p2.next;
p2.next = p1;
p1 = p2;
p2 = p3;
}
return p1;
}
public static ListNode reverse2(ListNode head){
ListNode newHead, temp;
newHead = null;
while(head!=null){
temp = head.next;
head.next = newHead;
newHead = head;
head = temp;
}
return newHead;
}