惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Threatpost
GbyAI
GbyAI
M
MIT News - Artificial intelligence
Apple Machine Learning Research
Apple Machine Learning Research
U
Unit 42
B
Blog
量子位
Scott Helme
Scott Helme
P
Proofpoint News Feed
NISL@THU
NISL@THU
Y
Y Combinator Blog
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
PCI Perspectives
PCI Perspectives
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
AI
AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Cloudbric
Cloudbric
L
LangChain Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
爱范儿
爱范儿
S
Secure Thoughts
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
AWS News Blog
AWS News Blog
C
Check Point Blog
Hacker News: Ask HN
Hacker News: Ask HN
雷峰网
雷峰网
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
V2EX - 技术
V2EX - 技术
Webroot Blog
Webroot Blog
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Cloudflare Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
O
OpenAI News
博客园 - 叶小钗
N
News | PayPal Newsroom
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 聂微东
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Vercel News
Vercel News
小众软件
小众软件

轶哥博客

blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog
blog
2020-05-16 · via 轶哥博客

LeetCode 25. K 个一组翻转链表 https://leetcode-cn.com/problems/reverse-nodes-in-k-group/
LintCode 450. K组翻转链表 https://www.lintcode.com/problem/reverse-nodes-in-k-group/

这是一道用常规思路就能解的算法题。目标清晰、题目易懂,不涉及复杂的算法。

按照题目要求,遍历链表,遍历的同时每经过k个节点就进行一次翻转。需要注意的是,第一次翻转后,记录下整个链表的head作为返回值。从第二次翻转开始,需要将之前翻转过的最后一个结点与新翻转后的第一个结点相连。

K个一组翻转链表

/**
 * @param {ListNode} head
 * @param {number} k
 * @return {ListNode}
 */
export const reverseKGroup = function (head, k) {
  let sum = 0 // 记录进行的结点个数
  let start = head // 记录每次翻转的第一个元素
  let res = head // 返回值:如果进行过翻转,则为第一次翻转的最后一个结点
  const queue = [] // 使用队列,方便连接上一次翻转的链表,最大长度为2
  while (head) { // 遍历一次链表
    if (++sum === k) { // 如果经过了k个结点,则翻转从start到head的一段结点
      const headNext = head.next
      queue.push(start) // 计入队列
      let next = head.next
      for (let i = 0; i < sum - 1; i++) { // 翻转结点
        const tmp = start.next
        start.next = next
        next = start
        start = tmp
      }

      start.next = next // 最后一个结点

      if (queue.length === 1) { // 判断是否为第一次翻转
        res = start
      } else {
        const la = queue.shift() // 连接上一次翻转的链表
        la.next = head
      }
      sum = 0 // 重置计数
      start = headNext
      head = headNext
    } else {
      head = head.next
    }
  }

  return res
}

leetcode-25

以往的算法题,暴力解法通常都无法通过测试,这题不需要什么操作技巧,就按题目要求的作解即可,编写过程中需要考虑清楚数据是如何变化的,做好边界条件的处理即可。

这道题还可以使用来解答。不妨再进一步思考,从后往前以k个为一组进行翻转如何实现?

这道题和合并K个排序链表算是链表中的少见的难题,其实这两道题整体思路都不复杂。

这是我的算法练习解题记录https://github.com/yi-ge/js-practice。欢迎加我微信探讨算法!