当前位置:网站首页>代码随想录笔记_链表_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),需要将指针的操作顺序在图上理顺。
边栏推荐
- Typora prompt [this beta version of typora is expired, please download and install a new version]
- Discuz论坛搭建详细过程,一看就懂
- How to configure env.js in multiple environments in uni app
- Un7.22: how to upload videos and pictures simultaneously with the ruoyi framework in idea and vs Code?
- How to import CAD files into the map new earth and accurately stack them with the image terrain tilt model
- Sword finger offer II 024. reverse linked list
- 【基于ROS的URDF练习实例】四轮机器人与摄像头的使用
- [FFH] websocket practice of real-time chat room
- Unity C tool class arrayhelper
- Tiktok shop will add a new site, and the Singapore site will be launched on June 9
猜你喜欢

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

How to configure env.js in multiple environments in uni app

C语言练习题目+答案:

Linked list - 24. Exchange nodes in the linked list in pairs

排序入门—插入排序和希尔排序

来阿里一年后我迎来了第一次工作变动....

FreeRTOS - use of software timer

0 threshold takes you to know two-point search

Six pictures show you why TCP shakes three times?
![[FFH] websocket practice of real-time chat room](/img/9a/ffd31fe8783804d40edeca515cc96c.png)
[FFH] websocket practice of real-time chat room
随机推荐
FreeRTOS - use of software timer
Meta tags let search engines search your website
2021 robocom world robot developer competition - undergraduate group (Preliminary) problem solution
Unity C tool class arrayhelper
First acquaintance with JVM
Relationship between fork and pipeline
Functions of tiktok enterprise number
剑指 Offer II 024. 反转链表
Cyclic multiple scatter
POI operation excel collation
Protocol buffers 的问题和滥用
Leetcode102-二叉树的层序遍历详解
Android系统安全 — 5.2-APK V1签名介绍
使用分区的优点
【FFH】OpenHarmony啃论文成长计划---cJSON在传统C/S模型下的应用
看了纪录片《埃达克岛的海盗宝藏》,想到我国历史上的遗失秘宝
CodeBlocks shortcut key operation Xiaoquan
Using OpenCV to do a simple face recognition
Source code analysis of BlockingQueue (arraybq and linkedbq)
基于FPGA的VGA字符显示