leetcode: Reverse Linked List

本文深入探讨了单链表逆转的实现方法,包括迭代和递归两种方式,详细分析了错误代码并给出了正确的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我去,单链表的逆转都没搞定,,,醉了

 

写的啥破玩意儿,,, 还是写不出来。

能出结果,但不知道哪里错了,没被ac, 太low了。

low写法:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

/*
思路:
1.
*/
// class Solution {
// public:
//     ListNode* reverseList(ListNode* head) {
     
//         ListNode* Ppre=head;
//          ListNode* Pcur=head->next;
//         //ListNode* Pnext=Pcur->next;
//          head->next=NULL;
            
//         if(NULL == Pcur->next){
//             Pcur->next=Ppre;
//             return Pcur;
//         }
//         else{
//             head=reverseList(Pcur);
//             // head->next=NULL;
//         return head;
//         }
       
//     }
// };

class Solution {
public:
    int length(ListNode* head) {
     
        int len=1;
        while(NULL!=head->next)
        {
            len++;
            head=head->next;
            //printf("%d  ",len);
        }
        return len;
        
    }
    
    
    ListNode* reverseList(ListNode* head) {
     ListNode* head_orig=head;
        int len=length(head);
        int arry[len];
       
        // vector<int>& nums;

        for(int i=0;i<len;i++)
        // while(NULL!=head)
        {
            arry[i]=head->val;
            head=head->next;
            printf("%d  ",arry[i]);
        }
        
        head=head_orig;
        len--;
        //for(int j=0;j<len+1;j++)
        while(NULL!=head)
        {
            printf("==%d  \n",arry[len]);
           head->val=arry[len--];
            head=head->next;
        }
        
        return head_orig;
        
    }
    
    
};

 

 

等下用正规的方法吧,,,

 

 

 

看了别人之后的写法;

/*
迭代思路:
1.空链表,或者只有一个元素,直接返回head
2.其他:
新定义几个中间变量,用头插法,
cur_head, head_next,head
*/


class Solution {
public:
    ListNode* reverseList(ListNode* head) {
     if(NULL==head || NULL==head->next) return head;
        
        ListNode *cur_head=NULL,*head_next=NULL;
        while(head)
        {
            head_next = head->next;//save next
            
            //头插,插在cur_head的前面
            head->next= cur_head;//point to current head, insert in front of 
            cur_head=head;//modify the first element,cur_head 修改最前面的头的位置,以便下次头插指向
            
            head=head_next;//修改未遍历的头
            
        }
        return cur_head;//返回最前面的头
    }
};

 

总结:

1.名字命名的不直观会特别影响理解和思路,,,努力把名字命名得简易直接明了。

2.分类讨论,简单化问题。

3.分析能力也太差了吧,先理清具体思路,每一步,再动手写。

 

 

醉了,递归也不会写。

//make your thinking clearly before coding
/*
递归思路:
1.走到最后一个元素之后,翻转指向
2.意味着,前面的每一层都得保存着,保存的信息应该包括 ,前一个节点,
3.分三类思考:分别分析判断,
第1个节点,最后将next域指向NULL(if后)
最后一个节点,返回本节点,并将next域指向head_pre
中间的普通节点,
递去:保存pre_node,head=head->next,head_orig
归来:(条件,到最后一个元素了) cur_node=reverse(head->next),cur_node->next=pre_node

*/
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
     
       ListNode *pre_save_node=NULL, *cur_node=NULL,*head_orig=NULL,*head_final=NULL;
         
        //if(NULL==head) return head;
        if( NULL==head->next)
        {//到最后了,归,只来一次
            head->next=pre_save_node;
            // head=pre_save_node;
            head_final=head;
            // cur_node=head->next;
             cur_node=pre_save_node;
            printf("comeback:%d\n",head->val);
            return cur_node;
        }
        
        pre_save_node=head;//存起来,后面作为下一个节点的next
        printf("comeback:%d\n",pre_save_node->val);
        cur_node=reverseList(head->next);
        printf("====\n");
        // printf("comeback:%d\n",cur_node->val);
        if(NULL!=head_orig) cur_node->next=head;
        // printf("====\n");
        return head_final;
       
    }
};

这么复杂还叫递归吗???

重新分析:

递归肯定是极度简洁的,

看别人家的:

//make your thinking clearly before coding
/*
递归思路:
1.走到最后一个元素之后,翻转指向
2.意味着,前面的每一层都得保存着,保存的信息应该包括 ,前一个节点,
3.分三类思考:分别分析判断,
第1个节点,最后将next域指向NULL(if后)
最后一个节点,返回本节点,并将next域指向head_pre
中间的普通节点,
递去:保存pre_node,head=head->next,head_orig
归来:(条件,到最后一个元素了) cur_node=reverse(head->next),cur_node->next=pre_node

*/
// class Solution {
// public:
//     ListNode* reverseList(ListNode* head) {
     
//        ListNode *pre_save_node=NULL, *cur_node=NULL,*head_orig=NULL,*head_final=NULL;
         
//         //if(NULL==head) return head;
//         if( NULL==head->next)
//         {//到最后了,归,只来一次
//             head->next=pre_save_node;
//             // head=pre_save_node;
//             head_final=head;
//             // cur_node=head->next;
//              cur_node=pre_save_node;
//             printf("comeback:%d\n",head->val);
//             return cur_node;
//         }
        
//         pre_save_node=head;//存起来,后面作为下一个节点的next
//         printf("comeback:%d\n",pre_save_node->val);
//         cur_node=reverseList(head->next);
//         printf("====\n");
//         // printf("comeback:%d\n",cur_node->val);
//         if(NULL!=head_orig) cur_node->next=head;
//         // printf("====\n");
//         return head_final;
       
//     }
// };



class Solution {
public:
    ListNode* reverseList(ListNode* head) {
      if(NULL==head || NULL==head->next) return head;
         ListNode *new_head=NULL,*head_next=NULL;
        head_next=head->next;
        head->next=NULL;//递归前切断
        new_head=reverseList(head_next);//递归
      head_next->next=head;//递归后反过来链接,改变指向
        return new_head;
    }
};


 

总结:肯定是过程没分析清,对递归根本不理解。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值