反正链表相关题目

灵神算法讲解

反正链表相关流程图processon

反转链表

题目描述

https://leetcode.cn/problems/reverse-linked-list/

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例一:

输入:head = [1,2,3,4,5] 输出:[5,4,3,2,1]

解题思路

迭代法

如果链表为空,或者链表只有一个结点,那么就可以直接返回头结点,因为此时不需要进行反转。否则的话,我们就通过迭代来进行反转,需要定义几个变量,pre,curr,next,对这几个结点来进行操作。

class Solution {
    public ListNode reverseList(ListNode head) {
        //如果链表为空,或者链表只有一个节点,则直接返回头结点
        if(head==null || head.next==null) return head;
        ListNode pre=null;
        ListNode curr=head;
        while(curr!=null){
            ListNode temp=curr.next;
            curr.next=pre;
            pre=curr;
            curr=temp;
        }
        return pre;
    }
}

递归法

一文读懂链表反转(迭代法和递归法) - 你是风儿 - 博客园

class Solution {
    public ListNode reverseList(ListNode head) {
         ListNode pre=null;
         return recur(head,pre);
    }
    /**
        反转两个结点
     */
    private ListNode recur(ListNode curr,ListNode pre){
        //终止条件
        if(curr==null) return pre;
        //递归后继结点
        ListNode res=recur(curr.next,curr);
        //修改节点引用指向
        curr.next=pre;
        //返回链表头结点
        return res;

    }

}

92. 反转链表 II

题目描述

解题思路

/**
 * 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 {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        if(head==null || head.next==null) return head;
        ListNode dummyNode=new ListNode(-1,head);
        ListNode p=dummyNode;
        //我们先找到left位置的前一个节点p
        int i=0;
        while(i<left-1){
            i++;
            p=p.next;
        }
        //这个时候先反转left到right位置的链表节点
        ListNode cur=p.next;
        ListNode pre=null;
       for(i=left;i<=right;i++){
            ListNode next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
       }
       //此时cur指向的反转链表的下一个节点
       p.next.next=cur;
       p.next=pre;
       return dummyNode.next;
    }
}

25. K 个一组翻转链表

题目描述

解题思路

/**
 * 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 {
    public ListNode reverseKGroup(ListNode head, int k) {
        //先求出链表长度
        int n=0;
        for(ListNode cur=head;cur!=null;cur=cur.next){
            n++;
        }
        //链表剩余长度>=k才需要进行反转
        ListNode dummyNode=new ListNode(-1,head);
        ListNode p=dummyNode;
        ListNode cur=head;
        for(;n>=k;n-=k){
            //反转这k个节点
            ListNode pre=null;
            for(int i=1;i<=k;i++){
                ListNode temp=cur.next;
                cur.next=pre;
                pre=cur;
                cur=temp;
            }
            ListNode temp=p.next;
            p.next.next=cur;
            p.next=pre;
            p=temp;
        }
        return dummyNode.next;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不断前进的皮卡丘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值