2021-07-20 LeetCode:反转链表

这篇博客探讨了如何使用迭代和递归方法在LeetCode中反转链表。内容包括完整反转链表,反转链表的前n个节点,以及在给定范围内的反转。博主提供了详细的代码实现,并解释了不同反转操作的关键步骤和逻辑。

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

LeetCode:反转链表

反转链表。
递归反转整个链表。可以使用迭代或递归来实现。
迭代或者递归的代码如下

class Solution {
    public ListNode reverseList(ListNode head) {
        //迭代
        // ListNode cur = null;
        // ListNode pre = head;
        // while(pre!=null){
        //     ListNode temp = pre.next;
        //     pre.next = cur;
        //     cur = pre;
        //     pre = temp;

        // }
        // return cur;

        
        //递归
        if(head == null){
            return head;
        }
        if(head.next == null){
            return head;
        }
        ListNode last = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return last;
    }
}

反转链表前n个节点

//后驱节点
ListNode successor = null;
public ListNode reverse(ListNode head ,int right){
   if(right == 1){
    //记录right+1个节点
     successor = head.next;
     return head;
    }
   ListNode last = reverse(head.next,right-1);
   head.next.next = head;
   //反转的节点和后面的节点连接起来
   head.next = successor;
   return last;
}

和反转所有链表不同的就是successor需要记录第n+1个节点,反转整个链表直接设置为null即可。

反转链表的一部分
递归
如果left=1,和上一种情况一样,相当于反转前n个元素。如果left!=1,那么将head的索引看作为1的话,就是想从第left个元素开始翻转,如果将head.next索引看作为1的话,就是从第left-1个元素开始翻转。
迭代
定位到要反转的第left-1个结点的位置,前驱结点每次都不会变,改变指针的指向。前面一个空结点是为了避免当left=1时的情况。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    //递归
    // //后驱节点
    // ListNode successor = null;
    // public ListNode reverse(ListNode head ,int right){
    //     if(right == 1){
    //         //记录right+1个节点
    //         successor = head.next;
    //         return head;
    //     }
    //     ListNode last = reverse(head.next,right-1);
    //     head.next.next = head;
    //     //反转的节点和后面的节点连接起来
    //     head.next = successor;
    //     return last;
    // }
    // public ListNode reverseBetween(ListNode head, int left, int right) {
    //     if(left == 1){
    //         return reverse(head,right);
    //     }
    //     head.next = reverseBetween(head.next,left-1,right-1);
    //     return head;
    // }
    //迭代
    public ListNode reverseBetween(ListNode head, int left, int right){
        ListNode dummy = new ListNode(0);
        int i=1;
        dummy.next = head;
        ListNode pre = dummy;
        for(;i<left;i++){
            pre = pre.next;
        }
        head = pre.next;
        for(i=left;i<right;i++){
            ListNode nex = head.next;
            head.next = nex.next;
            nex.next = pre.next;
            pre.next = nex;
            
        }
        return dummy.next;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值