当前位置:网站首页>代码随想录笔记_链表_25K个一组翻转链表
代码随想录笔记_链表_25K个一组翻转链表
2022-07-24 09:03:00 【Erik_Won】
代码随想录笔记_链表
代码随想录二刷笔记记录
LC25.K个一组翻转链表
懂车帝,频度3
题目
给你链表的头节点 head ,每 k 个节点一组进行翻转,请你返回修改后的链表。
k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
示例 1:

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

输入:head = [1,2,3,4,5], k = 3
输出:[3,2,1,4,5]
思路分析
思路:
利用dummy节点,遍历前 k 个节点,进行反转,再重新遍历第二轮 k 个节点,再进行反转即可。

代码实现
反转链表函数
public ListNode reverseList(ListNode head){
ListNode pre = null;
ListNode cur = head;
ListNode post = null;
while(cur != null){
post = cur.next;
cur.next = pre;
pre = cur;
cur = post;
}
return pre;
}
完整代码实现
public ListNode reverseKGroup(ListNode head, int k) {
if (null == head) return null;
//定义三个指针 dummy, pre, cur
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
ListNode cur = dummy;
while (cur.next != null){
for (int i = 0; i < k && cur != null; i++){
cur = cur.next;
}
if (cur == null) break;
//定义第四个指针 post 进行占位
ListNode post = cur.next;
//1.对前k个链表断链,准备反转
cur.next = null;
//2.反转k个链表
//定义第五个指针 start 记录 前k个节点的头节点
ListNode start = pre.next;
pre.next = reverseList(start);
//3.连接
start.next = post;
//4.指针前移
//把 pre 放在反转完毕的 k 个链表的最后一个节点,可以看作是新的dummy节点
pre = start;
//把 cur 放在反转完毕的 k 个链表的最后一个节点
cur = pre;
}
return dummy.next;
}
public ListNode reverseList(ListNode head){
//迭代
ListNode pre = null;
ListNode cur = head;
ListNode post = null;
while (cur != null){
post = cur.next;
cur.next = pre;
pre = cur;
cur = post;
}
return pre;
}
总结:
做了这些链表的题,总结以下解决链表问题时的常用方法:
虚拟头节点 dummy , 双指针(快慢指针) , 链表指针操作(CRUD)
链表指针操作:泛指之前采用的链接、断链、pre、current、post节点等操作,也可以简单的理解为链表操作,指针能够更好的在图上理解这类操作。
双指针:不仅用于链表、数组,其他数据结构也常用此方法,需要熟练掌握。
- 快慢指针:LC19删除链表倒数第N个节点 ,LC142环形链表II
- dummy 节点 + 链表指针操作:LC24两两交换链表中的节点,LC25K个一组翻转链表
- 链表指针操作:LC203移除链表元素,LC206反转链表,LC160相交链表
链表题目的难点,主要在于链表的操作,即有的时候需要涉及到多个指针(如 LC 25),需要将指针的操作顺序在图上理顺。
边栏推荐
- 阻塞队列BlockingQueue 源码解析(ArrayBQ和LinkedBQ)
- SDUT compilation principle experimental code
- Leetcode94-二叉树的中序遍历详解
- How should tiktok shop cooperate with live broadcast in the background?
- Rank 3 and count from 0 to 9. How many times does each number appear in total, and how many times does each number appear in the tens of hundreds,
- Seven data show the impact of tiktok's combination of payment and organic content
- 科目1-3
- UE5影视动画渲染MRQ分层学习笔记
- Porting boa server on imx6ull
- Rocky basics shell script Basics
猜你喜欢

Android系统安全 — 5.2-APK V1签名介绍

TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
![[example of URDF exercise based on ROS] use of four wheeled robot and camera](/img/c5/babce5c6921b9cb54f018dc83a3b87.jpg)
[example of URDF exercise based on ROS] use of four wheeled robot and camera

Some mistakes that Xiaobai often makes (update from time to time, and promise not to make them again next time)

【FFH】实时聊天室之WebSocket实战

【我的创作一周年纪念日】爱情是需要被纪念的,创作也是

Let's test 5million pieces of data. How to use index acceleration reasonably?

Rk3566 add project under external

03_ UE4 advanced_ illumination

Ubuntu20.04 install MySQL 5.7
随机推荐
使用分区的优点
Protocol buffers 的问题和滥用
[example of URDF exercise based on ROS] use of four wheeled robot and camera
From single architecture to distributed architecture, there are many pits and bugs!
Unity C tool class arrayhelper
C language practice questions + Answers:
How should tiktok shop cooperate with live broadcast in the background?
【FFH】OpenHarmony啃论文成长计划---cJSON在传统C/S模型下的应用
Redis learning - Introduction to redis and NiO principles
C # briefly describe the application of Richter's replacement principle
dp最长公共子序列详细版本(LCS)
林业调查巡检数据采集解决方案
[FFH] openharmony gnawing paper growth plan -- Application of cjson in traditional c/s model
Guys, what parameters can be set when printing flinksql so that the values can be printed? This later section is omitted. It's inconvenient. I read the configuration on the official website
How to import CAD files into the map new earth and accurately stack them with the image terrain tilt model
Basic use of Nacos (2) -- Nacos configuration center
DP longest common subsequence detailed version (LCS)
[translation] integration challenges in microservice architecture using grpc and rest
Attack and defense world ----- confusion1
Unity solves the package manager "you see to be offline"