Reverse a singly linked list.
Subscribe to see which companies asked this question
Discuss Notes
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL) return head;
ListNode* pp= dfs(head);
head->next=NULL;
return pp;
}
ListNode* dfs(ListNode * p)
{
if(p==NULL) return p;
if(p->next==NULL) return p;
ListNode* temp=p->next;
ListNode* pp=dfs(temp);
temp->next=p;
return pp;
}
};
589



被折叠的 条评论
为什么被折叠?



