Reverse Linked List QuestionEditorial Solution My Submissions Total Accepted: 132625 Total Submissions: 321899 Difficulty: Easy Reverse a singly linked list.
click to show more hints.
Subscribe to see which companies asked this question
解题思路:三个指针,不停移动。
/**
* 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) {
ListNode* prev = NULL;
ListNode* curr = head;
while(curr != NULL)
{
ListNode *temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
}
return prev;
}
};
转载请注明原文地址: https://ju.6miu.com/read-1300596.html