【Leetcode】25. K 个一组翻转链表

题目描述

在这里插入图片描述

// 25. K 个一组翻转链表


// 给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。
// k 是一个正整数,它的值小于或等于链表的长度。
// 如果节点总数不是 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; }
 * }
 */

// 双指针法
// 严格意义上是四指针,本题没有四个指针实现不了,翻转部分可以用
// 【Leetcode】206. 反转链表 中的方法。
// 现在以head = [1,2,3,4,5], k = 2 为例,方法过程如下:
//dummy 
// pre
// end 
// -1 -> 1 -> 2 -> 3 -> 4 -> 5 -> null
 
//dummy
// pre  end 
// -1 -> 1 -> 2 -> 3 -> 4 -> 5 -> null

//dummy
// pre       end 
// -1 -> 1 -> 2 -> 3 -> 4 -> 5 -> null

//dummy
// pre start end  Next
// -1 -> 1 -> 2 -> 3 -> 4 -> 5 -> null

//dummy
// pre start end  Next
// -1 -> 1 -> 2    3 -> 4 -> 5 -> null

//dummy
// pre  end start  Next
// -1    2 -> 1    3 -> 4 -> 5 -> null

//dummy
// pre  end start  Next
// -1 -> 2 -> 1    3 -> 4 -> 5 -> null

//dummy
// pre  end start  Next
// -1 -> 2 -> 1 -> 3 -> 4 -> 5 -> null

//           pre 
//dummy      end
// -1 -> 2 -> 1 -> 3 -> 4 -> 5 -> null
       
//dummy      pre  end
// -1 -> 2 -> 1 -> 3 -> 4 -> 5 -> null
       
//dummy      pre       end
// -1 -> 2 -> 1 -> 3 -> 4 -> 5 -> null
       
//dummy      pre start end  Next
// -1 -> 2 -> 1 -> 3 -> 4 -> 5 -> null
       
//dummy      pre start end  Next
// -1 -> 2 -> 1 -> 3 -> 4    5 -> null
       
//dummy      pre  end start Next
// -1 -> 2 -> 1 -> 4 -> 3    5 -> null

//dummy      pre  end start Next
// -1 -> 2 -> 1 -> 4 -> 3 -> 5 -> null

//                     pre
//dummy                end 
// -1 -> 2 -> 1 -> 4 -> 3 -> 5 -> null

// 执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
// 内存消耗:38.5 MB, 在所有 Java 提交中击败了82.93%的用户
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
		ListNode dummyHead = new ListNode(-1);
		dummyHead.next = head;
		
		ListNode pre = dummyHead;
		ListNode end = dummyHead;
		while (end.next != null) {
			for (int i = 0; i < k && end != null; i++) {
				end = end.next;
			}
			if (end == null)
				break;
			ListNode start = pre.next;
			ListNode endNext = end.next;
			end.next = null;
			pre.next = reverse(start);
			start.next = endNext;
			
			pre = start;
			end = start;
		}
		return dummyHead.next;
    }
	
	public ListNode reverse(ListNode head) {
		if (head == null || head.next == null)
			return head;
		ListNode pre = head;
		ListNode mid = head.next;
		ListNode cur = head.next.next;

		mid.next = pre;
		while (cur != null) {
			pre = mid;
			mid = cur;
			cur = cur.next;
			mid.next = pre;
		}
        return mid;
	}
}




// 或者可以这样写
// 执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
// 内存消耗:38.5 MB, 在所有 Java 提交中击败了84.39%的用户
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;

        ListNode lpre = dummyHead;
        ListNode l = lpre.next;
        ListNode rpre = dummyHead;
        while (rpre != null && rpre.next != null) {
            for (int i = 0; i < k; i++) {
                rpre = rpre.next;
                if (rpre == null) return dummyHead.next;
            }
            ListNode r = rpre.next;
            rpre.next = null;
            lpre.next = null;
            reverse(l);
            lpre.next = rpre;
            l.next = r;

            lpre = l;
            l = r;
            rpre = lpre;
        }

        return dummyHead.next;
    }   

    private void reverse(ListNode head) {
        if (head == null || head.next == null)
            return;
        ListNode left = head;
        ListNode mid = head.next;
        ListNode right = head.next.next;
        mid.next = left;
        while (right != null) {
            left = mid;
            mid = right;
            right = right.next;
            mid.next = left;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

锥栗

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

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

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

打赏作者

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

抵扣说明:

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

余额充值