leetcode 反转链表系列

leetcode 反转链表系列

单链表反转

206. 反转链表

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

img

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

输出:[5,4,3,2,1]

解析:主要有两种方法进行解决:

1. 递归

将除了头结点的子链表反转,然后将头结点放置到反转子链表的尾部。代码如下:

class Solution{
    public ListNode reverseList(ListNode head){
        if(head == null || head.next == null){
            return head;
		}
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
	}
}

2. 逐个反转

创建一个新的头结点,将原始链表的节点依次插入头部(个人看法:对于链表问题,不要吝啬变量的使用)。代码如下:

class Solution{
    public ListNode reverseList(ListNode head){
        ListNode newHead = null;
        ListNode cur = head;
        while(cur != null){
            ListNode tmp = cur.next;
            cur.next = newHead;
            newHead = cur;
            cur = tmp;
		}
        return newHead;
    }
}

反转链表2

92. 反转链表 II

给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。

img

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

1. 穿针引线(头插法,一次遍历)

代码如下:

class Solution{
    public ListNode reverseBetween(ListNode head, int left, int right){
        ListNode dummy = new ListNode(0, head);
        ListNode pre = dummy;
        for(int i=0; i<left-1; i++){
            pre = pre.next;//将结点定位到反转位之前,该节点在这里定住不动,后面的采用头插法一个一个反转
		}
        ListNode cur = pre.next;
        for(int i=0; i<right-left; i++){
            ListNode next = cur.next;
            cur.next = next.next;
            next.next = pre.next;
            pre.next = next;
		}
        return dummy.next;
    }
}

2. 将问题拆解

首先通过遍历将待反转区域择出来作为一个子链表进行反转,然后再将前后结点链接好。代码与反转链表类似,此处不予给出。


K个一组反转链表

25. K 个一组翻转链表

给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。

k 是一个正整数,它的值小于或等于链表的长度。

如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。

进阶:

你可以设计一个只使用常数额外空间的算法来解决此问题吗
你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。

img

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

1. 递归

将第一组拿出进行链表反转,将剩余的链表部分仍然按照k个一组进行反转,然后将两部分连接起来即可。代码如下:

class Solution{
    public ListNode reverseKGroup(ListNode head, int k){
        if(head == null || head.next == null){
            return head;
		}
        ListNode cur = head;
        for(int i=1; i<k && cur!=null; i++){
            cur = cur.next;
        }
        if(cur == null)return head;
        ListNode tmp = cur.next;
        cur.next = null;
        ListNode newHead = reverseList(head);
        ListNode newTmp = reverseKGroup(tmp, k);
        head.next = newTmp;
        return newHead;
	}
    public ListNode reverseList(ListNode head){
        if(head == null || head.next == null){
            return head;
		}
        ListNode newHead = null;
        ListNode cur = head;
        ListNode tmp = null;
        while(cur != null){
            tmp = cur.next;
            cur.next = newHead;
            newHead = cur;
            cur = tmp;
		}
        return newHead;
    }
}

bye~

bye~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值