<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Linked List on Yosgi</title><link>https://yosgi.github.io/zh/tags/linked-list/</link><description>Recent content in Linked List on Yosgi</description><generator>Hugo</generator><language>zh</language><lastBuildDate>Mon, 29 Jun 2026 09:20:52 +0000</lastBuildDate><atom:link href="https://yosgi.github.io/zh/tags/linked-list/index.xml" rel="self" type="application/rss+xml"/><item><title>反转链表2</title><link>https://yosgi.github.io/zh/post/%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A82/</link><pubDate>Fri, 23 Jul 2021 00:00:00 +1200</pubDate><guid>https://yosgi.github.io/zh/post/%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A82/</guid><description>考虑到头部也可能产生反转，所以需要一个虚拟头。 在遍历的过程中记录反转后的尾节点和之前的节点。 最后进行节点的链接处理。 var reverseBetween = function(head, m, n) { let noob = new ListNode(0) noob.next = head let cur = noob let index = 0 let A,B while (index &lt; m) { A = cur // A 记录反转子链的前一个节点 cur = cur.next index++ } B = cur // B 记录反转之后的尾节点 let pre = cur while (index &lt;= n) { let next = cur.next cur.next = pre pre = cur cur = next index++ } // 执行完后cur 指针在反转子链的下一个节点 B.next = cur A.next = pre return noob.next };</description></item><item><title>138-复制带随机指针的链表</title><link>https://yosgi.github.io/zh/post/138-%E5%A4%8D%E5%88%B6%E5%B8%A6%E9%9A%8F%E6%9C%BA%E6%8C%87%E9%92%88%E7%9A%84%E9%93%BE%E8%A1%A8/</link><pubDate>Thu, 22 Jul 2021 00:00:00 +1200</pubDate><guid>https://yosgi.github.io/zh/post/138-%E5%A4%8D%E5%88%B6%E5%B8%A6%E9%9A%8F%E6%9C%BA%E6%8C%87%E9%92%88%E7%9A%84%E9%93%BE%E8%A1%A8/</guid><description>拿到题目的想法是使用双指针指向新旧节点，再加上一个 map 用来映射旧节点和新节点。 递归 var listMap = new Map() var copyRandomList = function(head) { if(head === null) return head if (listMap.get(head)) { return listMap.get(head) } let newNode = new Node(head.val,null,null) listMap.set(head, newNode) newNode.next = copyRandomList(head.next) newNode.random = copyRandomList(head.random) return newNode };迭代 var copyRandomList = function(head) { if(head == null) return head var listMap = new Map() listMap.set(null,null) var cur = head while (cur!= null) { listMap.set(cur,new Node(cur.val,null,null)) cur = cur.next } cur = head while (cur!= null) { listMap.get(cur).next = listMap.get(cur.next) listMap.get(cur).random = listMap.get(cur.random) cur = cur.next } return listMap.get(head) };</description></item></channel></rss>