let 92. Reverse Linked List II

本文详细介绍了链表翻转的经典算法实现,包括完整的代码示例和解析。通过具体实例展示了如何进行链表的部分翻转,适用于理解链表操作的基础与进阶知识。

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

主题思想: 链表翻转:

链表翻转, 链表翻转经典代码:

public ListNode reverseList(ListNode head){

    if(head==null||head.next==null) return head;

    ListNode  prev=null,next=null;
    while(head.next!=null){
        next=head.next;
        head.next=prev;
        prev=head;
        head=next;
    }
    // important 
    head.next=prev;
    return head;
}

AC 代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {

        if(head==null||head.next==null) return head;

        ListNode next=null;
        ListNode prevNode=null;
        ListNode tail=null;
        ListNode pivot=head;
        int i=1;
        for(i=1;i<m;i++){
            if(pivot!=null){
                prevNode=pivot;
                pivot=pivot.next;
            }
        }
        tail=pivot;
        for(;i<n;i++){
            if(tail!=null)tail=tail.next;
        }
        if(tail!=null) next=tail.next;
        tail.next=null;
        tail=pivot;
        if(pivot==head) head=reverseList(pivot);
        else  prevNode.next=reverseList(pivot);
        tail.next=next;
        return head;
    }

    public ListNode reverseList(ListNode head){
        if(head==null) return head;
        ListNode prev=null;
        ListNode next=null;

        while(head.next!=null){
            next=head.next;
            head.next=prev;
            prev=head;
            head=next;
        }
        head.next=prev;
        return head;
    }
}

AC 代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {

        if(head==null||head.next==null) return head;

        ListNode next=null;
        ListNode prevNode=null;
        ListNode tail=null;
        ListNode pivot=head;
        int i=1;
        for(i=1;i<m;i++){
            if(pivot!=null){
                prevNode=pivot;
                pivot=pivot.next;
            }
        }
        tail=pivot;
        for(;i<n;i++){
            if(tail!=null)tail=tail.next;
        }
        if(tail!=null) next=tail.next;
        tail.next=null;
        tail=pivot;
        if(pivot==head) head=reverseList(pivot);
        else  prevNode.next=reverseList(pivot);
        tail.next=next;
        return head;
    }

    public ListNode reverseList(ListNode head){
        if(head==null) return head;
        ListNode prev=null;
        ListNode next=null;

        while(head.next!=null){
            next=head.next;
            head.next=prev;
            prev=head;
            head=next;
        }
        head.next=prev;
        return head;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值