原题链接:https://leetcode.com/problems/reverse-linked-list/
原题:Reverse a singly linked list.
题意:要求你反转一个链表
思路:就是反转一个单向链表,三个指针解决,没啥好说的。
AC代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL) return NULL;
else if(head->next==NULL) return head;
else{
ListNode* pre = head;
ListNode* midle=head -> next;
ListNode* last=head -> next -> next;
head->next=NULL;
while(last != NULL){
midle->next = pre;
pre = midle;
midle = last;
last = last->next;
}
midle->next = pre;
return midle;
}
return NULL;
}
};