LeetCode-探索-初级算法-链表-3. 反转链表(个人做题记录,不是习题讲解)

50 篇文章 0 订阅
50 篇文章 0 订阅
这篇博客记录了作者在LeetCode中解决初级算法问题——链表反转的个人经历。作者采用了非递归的3指针方法,并提供了参考代码和自己的重写版本,详细解释了思路和逻辑,包括如何处理边界条件和递归过程。
摘要由CSDN通过智能技术生成
LeetCode-探索-初级算法-链表-3. 反转链表(个人做题记录,不是习题讲解)

LeetCode探索-初级算法:https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/

  1. 反转链表
  • 语言:java

  • 思路:一开始没想好,后面看了下网上的思路,大致就是用多个指针,往后遍历的时候顺便就修改next了。用3个指针分别表示当前,上一个,下一个

  • 代码(0ms):

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode reverseList(ListNode head) {
            ListNode pre = head,current = head,next = head;
            if(next == null)
                return null;
            next = next.next;
            pre.next = null;
            while(next!=null){
                pre = current;
                current = next;
                next = next.next;
                current.next = pre;
            }
            return current;
        }
    }
    
  • 参考代码1(0ms)非递归:要说的话也是3指针,但是它这个判断更统一,更简练。

    class Solution {
        public ListNode reverseList(ListNode head) {
            ListNode curr = head;
            ListNode prev = null;
            
            while (curr != null) {
                ListNode temp = curr.next;
                curr.next = prev;
                prev = curr;
                curr = temp;
            }
            
            return prev;
        }
    }
    
  • 参考代码2(8ms)递归:

    https://blog.csdn.net/xiashanrenlaozhang/article/details/80834701

    https://www.cnblogs.com/tengdai/p/9279421.html

    class Solution {
    public:
        ListNode* reverseList(ListNode* head) {
            // 采用递归的方法,就是使用反转函数反转子链表,直接当做已经反转了。
            if((head == NULL) || (head->next == NULL)){
                return head;
            }
            else{
                ListNode* son_list = reverseList(head->next);
                // 子链表的尾节点指向前一个节点
                head->next->next = head;
                head->next = NULL;
                return son_list;
            }
        }
    };
    
  • 参考后重写1(0ms)非递归:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode reverseList(ListNode head) {
            ListNode pre = null,current = head;
            while(current!=null){
                ListNode tmp = current.next;
                current.next = pre;
                pre = current;
                current = tmp;
            }
            return pre;
        }
    }
    
  • 参考后重写2(0ms)递归:先判断如果当前节点A是null或其下一节点是null,说明不用反转or已经反转完了,直接返回当前节点A;递归部分:获取A的下一个节点B,由于B要作为下一个递归的A节点,所以用临时变量C=B传入递归函数,B的next=A,A的next=null,返回C;

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode reverseList(ListNode head) {
            if(head==null||head.next==null){
                return head;
            }
            ListNode B = head.next;
            ListNode nextHead = reverseList(B); 
            B.next = head;
            head.next = null;
            return nextHead;
        }
    }
    
  • 参考后重写2:递归肯定向后,所以最后到最后一个节点时返回;递归时,每次当前A的下一个节点B的next->A,而A的next->null;因为需要把当前B节点当作下次的A节点,所以使用res递归传入B作为下次递归的A;这样res一定会是最后一个节点,所以返回res

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode reverseList(ListNode head) {
            if(head==null||head.next==null){
                return head;
            }
            ListNode res = reverseList(head.next);
            head.next.next = head;
            head.next = null;
            return res;
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值