LeetCode-206
Links:https://leetcode.com/problems/reverse-linked-list/
Reverse a singly linked list.
思路:
A->B->C->D
3个指针分别指向ABC,令A->NULL
然后做循环:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/** * 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 || head->next == NULL)return head; ListNode* temp = head->next; ListNode* temp2 = temp->next; head->next = NULL; while(1) { temp->next = head; head = temp; temp = temp2; if(temp!=NULL)temp2 = temp->next; else return head; } } }; |
【LeetCode】206. Reverse Linked List