/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The head of linked list.
* @return: The new head of reversed linked list.
*/
public ListNode reverse(ListNode head) {
ListNode p;
ListNode q;
ListNode node;
if(head==null){
return head;
}
if(head!=null){
p=head.next;
if(p==null){
return head;
}else{
if(p.next==null){
head.next=null;
p.next=head;
return p;
}
}
}
node=head;
p=node.next;
q=p.next;
node.next=null;
p.next=node;
while(q!=null){
node=q.next;
q.next=p;
p=q;
q=node;
}
return p;
}
}