/**
* Definition of ListNode
*
* class ListNode {
* public:
* int val;
* ListNode *next;
*
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: The first node of linked list.
* @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* r=new ListNode(0);
ListNode* tmp=r;
ListNode* tmp2=NULL;
while(tmp!=NULL){
tmp=head->next;
head->next=r->next;
r->next=head;
head=tmp;
}
return r->next;
}
};
* Definition of ListNode
*
* class ListNode {
* public:
* int val;
* ListNode *next;
*
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: The first node of linked list.
* @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* r=new ListNode(0);
ListNode* tmp=r;
ListNode* tmp2=NULL;
while(tmp!=NULL){
tmp=head->next;
head->next=r->next;
r->next=head;
head=tmp;
}
return r->next;
}
};