[LeetCode] 25. K 个一组翻转链表 ☆☆☆☆☆(链表)
描述
给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。
k 是一个正整数,它的值小于或等于链表的长度。
如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
示例 :
给定这个链表:1->2->3->4->5
当 k = 2 时,应当返回: 2->1->4->3->5
当 k = 3 时,应当返回: 3->2->1->4->5
说明 :
你的算法只能使用常数的额外空间。
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
解析
遍历出k个节点,然后翻转子链表,再接到原链表上。
代码
//my
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || k <= 1)
return head; int temp = k;
ListNode p = head;
while (temp > 1) {
if (p == null)
break;
p = p.next;
temp--;
}
if (p == null)
return head; ListNode cur_pre = head;
ListNode next_pre = p.next;
p.next = null; ListNode newHead = reverse(cur_pre);//此时cur_pre.next = null,即cur_pre为新子链表的最后一个节点
p = cur_pre;
head = newHead;
p.next = reverseKGroup(next_pre, k);
return head;
} public ListNode reverse(ListNode head) {
if (head == null) {
return head;
}
ListNode pre = null;
ListNode cur = head;
while (cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
//注意2个节点的翻转链表即可
public ListNode reverseKGroup(ListNode head, int k) {
if (k <= 0) return head;
int temp = k;
ListNode p = head;
while (temp > 1) {
if (p == null) break;
p = p.next;
temp--;
}
if (p == null) return head;
ListNode cur_pre = head;
ListNode pNext_pre = p.next;
ListNode newHead = reverseList(head, p);
head = newHead;
p = cur_pre;
p.next = reverseKGroup(pNext_pre, k);
return head;
} public ListNode reverseList(ListNode head, ListNode endNode) {
ListNode pre = null;
ListNode cur = head;
ListNode end = endNode.next;
while (cur != end) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
[LeetCode] 25. K 个一组翻转链表 ☆☆☆☆☆(链表)的更多相关文章
- Java实现 LeetCode 25 K个一组翻转链表
25. K 个一组翻转链表 给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度. 如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持 ...
- leetcode 25. K 个一组翻转链表
# coding:utf-8 __author__ = "sn" """ 25. K 个一组翻转链表 给你一个链表,每 k 个节点一组进行翻转,请你返 ...
- LeetCode 25. K 个一组翻转链表 | Python
25. K 个一组翻转链表 题目来源:https://leetcode-cn.com/problems/reverse-nodes-in-k-group 题目 给你一个链表,每 k 个节点一组进行翻转 ...
- [LeetCode] 25. k个一组翻转链表
题目链接: https://leetcode-cn.com/problems/reverse-nodes-in-k-group/ 题目描述: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链 ...
- LeetCode 25 —— K 个一组翻转链表
1. 题目 2. 解答 首先,利用快慢指针确定链表的总结点数. 偶数个结点时,结点个数等于 i * 2. 奇数个结点时,结点个数等于 i * 2 + 1. 然后将链表的每 K 个结点划分为一组.循环对 ...
- LeetCode 25. k个一组翻转链表(Reverse Nodes in k-Group)
题目描述 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. 示例 : 给定 ...
- leetcode 24. 两两交换链表中的节点 及 25. K 个一组翻转链表
24. 两两交换链表中的节点 问题描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2-> ...
- [链表]LeetCode 25 K组一个翻转链表
LeetCode 25 k组一个翻转链表 TITLE 示例 1: 输入:head = [1,2,3,4,5], k = 2 输出:[2,1,4,3,5] 示例 2: 输入:head = [1,2,3, ...
- 25. k个一组翻转链表
题目描述 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. 示例 : 给定 ...
随机推荐
- Nginx记录-Proxy_pass多个应用配置(转载)
1. 在http节点下,加入upstream节点. upstream linuxidc { server 10.0.6.108:7080; server 10.0.0.85:8 ...
- LD SCore计算基因多效性、遗传度、遗传相关性(the LD Score regression intercept, heritability and genetic correlation)
这篇文章是对之前啊啊救救我,为何我的QQ图那么飘(全基因组关联分析)这篇文章的一个补坑. LD SCore除了查看显著SNP位点对表型是否为基因多效性外,还额外补充了怎么计算表型的遗传度和遗传相关性. ...
- nginx配置ssl证书,启动http访问并代理到本地http端口
小白第一次使用nginx,本地环境Ubuntu 16.04.6 1.安装Nginx sudo apt install nginx 2.生成证书 (参考来源:https://segmentfault.c ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode] 249. Group Shifted Strings 分组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [LeetCode] 293. Flip Game 翻转游戏
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [LeetCode] 360. Sort Transformed Array 排序转换后的数组
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- 【SSH进阶之路】Spring的IOC逐层深入——Spring的IOC原理[通俗解释一下](三)
1. IoC理论的背景我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们打开机械 ...
- 「模拟赛20191019」B 容斥原理+DP计数
题目描述 将\(n\times n\)的网格黑白染色,使得不存在任意一行.任意一列.任意一条大对角线的所有格子同色,求方案数对\(998244353\)取模的结果. 输入 一行一个整数\(n\). 输 ...
- Mac OS备份迁移iBooks图书操作方法
前段时间换电脑,需要把原本电脑上的一些文件备份.迁移出来,包括iBooks中的电子书. 理论上,苹果体系中通过icloud账号可以把通讯录.备忘录等东西同步过去,但查了一下发现图书支持有限,而且我的e ...