Reverse Linked ListTotal accepted:1726 Total submissions:4378my submissions QuestionSolution
Reverse a singly linked list.
Click to show more hints.
Hide TagsLinked ListHas you met this question in a real interview? Yes No
Discuss
This problem is relatively simple, mainly to the list of reverse order, so you can set three pointers, Ptr0,ptr1 point to the two nodes to be exchanged, PTR2 before the exchange point to ptr1 the next node,
This is good for the next exchange to prepare, that is, to move back on it, until the end
#include <iostream>using namespace std;struct listnode {int val; ListNode *next; ListNode (int x): Val (x), Next (NULL) {}}; listnode* reverselist (listnode* head) {if (head==null| | Head->next==null) return head; listnode* Ptr0=head; listnode* ptr1=head->next; listnode* Ptr2;ptr0->next=null;while (1) {ptr2=ptr1->next;ptr1->next=ptr0;if (Ptr2==NULL) break;ptr0=ptr1; PTR1=PTR2;} return PTR1;} int main () {}
leetcode_206 title--reverse Linked list (linked list)