链表反转 递归写法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014737310/article/details/51371429

206. Reverse Linked List

   My Submissions
Total Accepted: 107857 Total Submissions: 271807 Difficulty: Easy

Reverse a singly linked list.

click to show more hints.

Subscribe to see which companies asked this question

Show Tags
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;
    }
};




展开阅读全文

没有更多推荐了,返回首页