LeetCode|Reverse Linked List
【问题描述】
Reverse a singly linked list.
【解答】
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL || head->next==NULL)
return head;
ListNode* pre=NULL;
ListNode* cur=head;
while(cur!=NULL)
{
ListNode* nt=cur->next;
cur->next=pre;
pre=cur;
cur=nt;
}
return pre;
}
};
这道题不会,考虑了好久,绕不过来~看了答案写的,最初返回的时候还写成返回cur,仔细观察了一下,应该是返回pre。
还没有评论,来说两句吧...