Total Accepted: 107857
Total Submissions: 271807
Difficulty: Easy
Reverse a singly linked list.
Subscribe to see which companies asked this question
Show Similar Problems
Have you met this question in a real interview?
Yes
No
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;
}
};
本文介绍了一种通过递归方式实现链表反转的方法。利用深度优先搜索的思想,从链表尾部开始逐步将节点指向前一个节点,最终完成整个链表的反转。此算法为链表操作提供了一个简洁高效的解决方案。
1604

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



