/**
* Definition of ListNode
*
* class ListNode {
* public:
* int val;
* ListNode *next;
*
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param head: n
* @return: The new head of reversed linked list.
*/
ListNode * reverse(ListNode * head) {
// write your code here
if(head==NULL||head->next==NULL){
return head;
}
ListNode * p,*q,*t;
p=head;
q=head->next;
t=q->next;
head->next=NULL;
if(t==NULL){
q->next=p;
return q;
}
while(1){
q->next=p;
p=q;
q=t;
t=q->next;
if(t==NULL){
q->next=p;
break;
}
}
return q;
}
};
* Definition of ListNode
*
* class ListNode {
* public:
* int val;
* ListNode *next;
*
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param head: n
* @return: The new head of reversed linked list.
*/
ListNode * reverse(ListNode * head) {
// write your code here
if(head==NULL||head->next==NULL){
return head;
}
ListNode * p,*q,*t;
p=head;
q=head->next;
t=q->next;
head->next=NULL;
if(t==NULL){
q->next=p;
return q;
}
while(1){
q->next=p;
p=q;
q=t;
t=q->next;
if(t==NULL){
q->next=p;
break;
}
}
return q;
}
};