问题描述:
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
原问题链接:https://leetcode.com/problems/reverse-nodes-in-k-group/
问题分析
这个问题并不是多难,而是整个调整的过程比较繁琐。
总体的思路如下,每次设定一个指针pilot往前面移动k步,然后在pilot后面的元素全部都翻转过来。要处理的细节就是翻转链表以及在移动k步的时候如果已经到结尾了要退出循环返回。详细实现的代码如下:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode reverseKGroup(ListNode head, int k) { if (head == null || head.next == null || k < 2) return head; ListNode dummy = new ListNode(0); dummy.next = head; ListNode pre = dummy, cur = head; while(cur != null) { ListNode pilot = pre.next; int i = 0; for(i = 0; i < k && pilot != null; i++) pilot = pilot.next; if (i < k) break; while(cur.next != pilot) { ListNode nt = cur.next.next; cur.next.next = pre.next; pre.next = cur.next; cur.next = nt; } pre = cur; cur = cur.next; } return dummy.next; } }
相关推荐
不会LeetCode_532--K-diff-Pairs-in-an-Array 给定一个整数数组和一个整数 k,您需要找到数组中唯一 k-diff 对的数量。 这里 k-diff 对定义为整数对 (i, j),其中 i 和 j 都是数组中的数字,它们的绝对差为 k。 示例...
leetcode题库所有数据库问题 Leetcode 所有数据库问题:Leetcode 问题 Active-Businesses-LeetCode.png 活跃用户-LeetCode.png 活动-参与者-LeetCode.png 至少合作过三次的演员和导演-LeetCode.png Ads-Performance-...
LeetCode-Dagon LeetcodeTop100:fire: 23.合并K个排序链表 32.最长有效括号 \ 94.二叉树的中序遍历 力扣每日一题 leetcode周赛 第184周 List :pushpin: :pushpin: :pushpin: Type :pushpin: :green_heart::balloon: ...
leetcode 530 530.-BST-Leetcode 中的最小绝对差 给定二叉搜索树 (BST) 的根,返回树中任意两个不同节点的值之间的最小绝对差。 示例 1: 输入:root = [4,2,6,1,3] 输出:1 示例 2: 输入:root = [1,0,48,null,...
reverse-nodes-in-k-group: 解析 pre_for_next 到辅助函数 29:除以两个整数:溢出; 两反 31:下一个排列:再做一次(排序!) 32:最长有效(),使用栈,左推idx 33: search-in-rotated-sorted-array ,比较中间值...
leetcode 第321题Reversed_Integer Java中的LeetCode逆整数问题解决方案 问题 - 给定一个有符号的 32 位整数 x,返回 x 其数字颠倒。 如果反转 x 导致值超出有符号的 32 位整数范围 [-231, 231 - 1],则返回 0。 ...
leetcode添加元素使和等于 LeetCode 数组 Leetcode 0004 寻找两个正序数组的中位数 ----> ----> Leetcode 0027 移除元素 ----> ----> Leetcode 0041 缺失的第一个正数 ----> ----> Leetcode 0048 ...
代码:// find K-th Smallest Pair Distancepublic int smallestDistancePair(int[] nums
leetcode题库 leetcode 生成文件工具 Usage: python tool.py -p [name] [options] Options: -h, --h 查看帮助 -p name leetcode 题目编号,必须 -a 题目类型为算法 -d 题目类型为数据库 -s 题目类型为Shell -c 编程...
LeetCode LeetCode解决了问题-Python3
第 338 章338.-Counting-Bits-LeetCode 计数位-LeetCode
注意:如果您使用的是leetcode-cn.com ,则可以忽略此部分。 最近我们观察到。 此问题的根本原因是 leetcode.com 更改了其登录机制,目前还没有理想的方法来解决该问题。 感谢您提供了一种可以以某种方式缓解这种...
leetcode中国Make-Dir-For-Leetcode 演示 什么 这个脚本会为 LeetCode 创建一个文件夹,在每个子文件夹中会有一个名为README.md的文件,这个文件的内容就是 LeetCode 对应的问题内容。 WHO 如果你想使用 LeetCode 来...
leetcode提交记录消失解决leetcode问题 日志 模板: - id : type : difficulty : easy url : first-submission-successful : yes 无法解决 2018-05-31 : - id : 13 type : string difficulty : easy url : ...
Algorithm-In-Leetcode To solve algorithm problems in leetcode.com using Python, Golang and SQL It's my leetcode account: 这是我的LeetCode中文账户: 不仅仅是Algorithm,还有Database的题目也包含在内。题目...
https://leetcode-cn.com/problems/two-sum/ 难度 : 简单 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。...
leetcode有效LeetCode-1108.-Defanging-an-IP-Address 给定一个有效的 (IPv4) IP 地址,返回该 IP 地址的去角版本。 去污点的 IP 地址会替换每个句点“.”。 和 ”[。]”。 示例 1: 输入:address = "1.1.1.1" 输出...
leetcode 走踏板LeetCode_70--爬楼梯 你正在爬楼梯。 需要n步才能到达顶部。 每次您可以爬 1 或 2 个台阶。 你可以通过多少种不同的方式登上顶峰? 注意:给定 n 将是一个正整数。 示例 1: 输入:2 输出:2 说明:...
java lru leetcode Leetcode-Java实现,记录易理解或最优的解法 此仓库会频繁更新,推荐 star 或 watch以即时得到更新通知 刷就完事
leetcode104 qijin-tech-leetcode 2019第15周之前 LeetCode104. 典型的DP问题。用递归方法最简单 LeetCode136. 相同为0,不同为1。想到了什么?异或操作啊 LeetCode2. 链表题。逻辑难度不大,细节很多。 LeetCode771...