几道常见的链表算法题

讨论 未结 精帖 0 3357
emailuser_bhgkg
emailuser_bhgkg LV1 2022年12月16日 10:58 编辑
<section id="nice" style="font-size: 16px; padding-right: 10px; padding-left: 10px; word-break: break-word; overflow-wrap: break-word; line-height: 1.25; font-family: Optima-Regular, Optima, PingFangTC-Light, PingFangSC-light, PingFangTC-light; letter-spacing: 2px; background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.05) 3%, rgba(0, 0, 0, 0) 3%); background-size: 20px 20px; background-position: center center;"><h2 style="font-weight: bold; font-size: 22px; margin-top: 20px; margin-right: 10px; margin-bottom: 0px;"><span style="font-size: 18px; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(145, 109, 213);">几道常见的链表算法题</span></h2> <h2 style="font-weight: bold; font-size: 22px; margin-top: 20px; margin-right: 10px; margin-bottom: 0px;"><span style="font-size: 18px; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(145, 109, 213);">1. 两数相加</span></h2> <h3 style="margin-top: 30px; margin-bottom: 15px; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #d89cf6;">题目描述</span></h3> <blockquote style="border-top: none; border-right: none; border-bottom: none; border-image: initial; font-size: 0.9em; overflow: auto; border-left-width: 3px; color: rgb(106, 115, 125); padding-top: 10px; padding-bottom: 10px; padding-left: 20px; margin-bottom: 20px; margin-top: 20px; border-left-color: rgb(216, 156, 246); background: rgb(244, 238, 255);"> <p style="padding-top: 8px; padding-bottom: 8px; font-size: 14px; word-spacing: 2px; margin-top: 0px; margin-bottom: 0px; color: black; line-height: 26px;">Leetcode:给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。</p> <p style="padding-top: 8px; padding-bottom: 8px; font-size: 14px; word-spacing: 2px; margin-top: 0px; margin-bottom: 0px; color: black; line-height: 26px;">你可以假设除了数字 0 之外,这两个数字都不会以零开头。</p> </blockquote> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">示例:</p> <pre><code style="overflow-x: auto; padding: 16px; color: rgb(171, 178, 191); background: rgb(40, 44, 52); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; border-radius: 0px;">输入:(2&nbsp;-&gt;&nbsp;4&nbsp;-&gt;&nbsp;3)&nbsp;+&nbsp;(5&nbsp;-&gt;&nbsp;6&nbsp;-&gt;&nbsp;4)<br>输出:7&nbsp;-&gt;&nbsp;0&nbsp;-&gt;&nbsp;8<br>原因:342&nbsp;+&nbsp;465&nbsp;=&nbsp;807<br></code></pre> <h3 style="margin-top: 30px; margin-bottom: 15px; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #d89cf6;">问题分析</span></h3> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">Leetcode 官方详细解答地址:</p> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">https://leetcode-cn.com/problems/add-two-numbers/solution/</p> <blockquote style="border-top: none; border-right: none; border-bottom: none; border-image: initial; font-size: 0.9em; overflow: auto; border-left-width: 3px; color: rgb(106, 115, 125); padding-top: 10px; padding-bottom: 10px; padding-left: 20px; margin-bottom: 20px; margin-top: 20px; border-left-color: rgb(216, 156, 246); background: rgb(244, 238, 255);"> <p style="padding-top: 8px; padding-bottom: 8px; font-size: 14px; word-spacing: 2px; margin-top: 0px; margin-bottom: 0px; color: black; line-height: 26px;">要对头结点进行操作时,考虑创建哑节点 dummy,使用 dummy-&gt;next 表示真正的头节点。这样可以避免处理头节点为空的边界问题。</p> </blockquote> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">我们使用变量来跟踪进位,并从包含最低有效位的表头开始模拟逐 位相加的过程。</p> <h3 style="margin-top: 30px; margin-bottom: 15px; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #d89cf6;">Solution</span></h3> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;"><strong style="color: rgb(145, 109, 213);">「我们首先从最低有效位也就是列表 l1 和 l2 的表头开始相加。注意需要考虑到进位的情况!」</strong></p> <pre><code style="overflow-x: auto; padding: 16px; color: rgb(171, 178, 191); background: rgb(40, 44, 52); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; border-radius: 0px;"><span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br>&nbsp;*&nbsp;Definition&nbsp;for&nbsp;singly-linked&nbsp;list.<br>&nbsp;*&nbsp;public&nbsp;class&nbsp;ListNode&nbsp;{<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;val;<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;next;<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ListNode(int&nbsp;x)&nbsp;{&nbsp;val&nbsp;=&nbsp;x;&nbsp;}<br>&nbsp;*&nbsp;}<br>&nbsp;*/</span><br>&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//https://leetcode-cn.com/problems/add-two-numbers/description/</span><br><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span>&nbsp;<span style="color: #e6c07b; line-height: 26px;">Solution</span>&nbsp;</span>{<br><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span>&nbsp;ListNode&nbsp;<span style="color: #61aeee; line-height: 26px;">addTwoNumbers</span><span style="line-height: 26px;">(ListNode&nbsp;l1,&nbsp;ListNode&nbsp;l2)</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;dummyHead&nbsp;=&nbsp;<span style="color: #c678dd; line-height: 26px;">new</span>&nbsp;ListNode(<span style="color: #d19a66; line-height: 26px;">0</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;p&nbsp;=&nbsp;l1,&nbsp;q&nbsp;=&nbsp;l2,&nbsp;curr&nbsp;=&nbsp;dummyHead;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//carry&nbsp;表示进位数</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">int</span>&nbsp;carry&nbsp;=&nbsp;<span style="color: #d19a66; line-height: 26px;">0</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">while</span>&nbsp;(p&nbsp;!=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>&nbsp;||&nbsp;q&nbsp;!=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">int</span>&nbsp;x&nbsp;=&nbsp;(p&nbsp;!=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>)&nbsp;?&nbsp;p.val&nbsp;:&nbsp;<span style="color: #d19a66; line-height: 26px;">0</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">int</span>&nbsp;y&nbsp;=&nbsp;(q&nbsp;!=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>)&nbsp;?&nbsp;q.val&nbsp;:&nbsp;<span style="color: #d19a66; line-height: 26px;">0</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">int</span>&nbsp;sum&nbsp;=&nbsp;carry&nbsp;+&nbsp;x&nbsp;+&nbsp;y;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//进位数</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;carry&nbsp;=&nbsp;sum&nbsp;/&nbsp;<span style="color: #d19a66; line-height: 26px;">10</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//新节点的数值为sum&nbsp;%&nbsp;10</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curr.next&nbsp;=&nbsp;<span style="color: #c678dd; line-height: 26px;">new</span>&nbsp;ListNode(sum&nbsp;%&nbsp;<span style="color: #d19a66; line-height: 26px;">10</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curr&nbsp;=&nbsp;curr.next;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">if</span>&nbsp;(p&nbsp;!=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>)&nbsp;p&nbsp;=&nbsp;p.next;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">if</span>&nbsp;(q&nbsp;!=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>)&nbsp;q&nbsp;=&nbsp;q.next;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">if</span>&nbsp;(carry&nbsp;&gt;&nbsp;<span style="color: #d19a66; line-height: 26px;">0</span>)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curr.next&nbsp;=&nbsp;<span style="color: #c678dd; line-height: 26px;">new</span>&nbsp;ListNode(carry);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">return</span>&nbsp;dummyHead.next;<br>}<br>}<br></code></pre> <h2 style="font-weight: bold; font-size: 22px; margin-top: 20px; margin-right: 10px; margin-bottom: 0px;"><span style="font-size: 18px; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(145, 109, 213);">2. 翻转链表</span></h2> <h3 style="margin-top: 30px; margin-bottom: 15px; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #d89cf6;">题目描述</span></h3> <blockquote style="border-top: none; border-right: none; border-bottom: none; border-image: initial; font-size: 0.9em; overflow: auto; border-left-width: 3px; color: rgb(106, 115, 125); padding-top: 10px; padding-bottom: 10px; padding-left: 20px; margin-bottom: 20px; margin-top: 20px; border-left-color: rgb(216, 156, 246); background: rgb(244, 238, 255);"> <p style="padding-top: 8px; padding-bottom: 8px; font-size: 14px; word-spacing: 2px; margin-top: 0px; margin-bottom: 0px; color: black; line-height: 26px;">剑指 offer:输入一个链表,反转链表后,输出链表的所有元素。</p> </blockquote> <h3 style="margin-top: 30px; margin-bottom: 15px; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #d89cf6;">问题分析</span></h3> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">这道算法题,说直白点就是:如何让后一个节点指向前一个节点!在下面的代码中定义了一个 next 节点,该节点主要是保存要反转到头的那个节点,防止链表 “断裂”。</p> <h3 style="margin-top: 30px; margin-bottom: 15px; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #d89cf6;">Solution</span></h3> <pre><code style="overflow-x: auto; padding: 16px; color: rgb(171, 178, 191); background: rgb(40, 44, 52); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; border-radius: 0px;"><span style="color: #c678dd; line-height: 26px;">public</span>&nbsp;<span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span>&nbsp;<span style="color: #e6c07b; line-height: 26px;">ListNode</span>&nbsp;</span>{<br>&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">int</span>&nbsp;val;<br>&nbsp;&nbsp;ListNode&nbsp;next&nbsp;=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>;<br><br>&nbsp;&nbsp;ListNode(<span style="color: #c678dd; line-height: 26px;">int</span>&nbsp;val)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">this</span>.val&nbsp;=&nbsp;val;<br>&nbsp;&nbsp;}<br>}<br></code></pre> <pre><code style="overflow-x: auto; padding: 16px; color: rgb(171, 178, 191); background: rgb(40, 44, 52); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; border-radius: 0px;"><span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br>&nbsp;*<br>&nbsp;*&nbsp;<span style="color: #c678dd; line-height: 26px;">@author</span>&nbsp;Snailclimb<br>&nbsp;*&nbsp;<span style="color: #c678dd; line-height: 26px;">@date</span>&nbsp;2018年9月19日<br>&nbsp;*&nbsp;<span style="color: #c678dd; line-height: 26px;">@Description</span>:&nbsp;TODO<br>&nbsp;*/</span><br><span style="color: #c678dd; line-height: 26px;">public</span>&nbsp;<span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span>&nbsp;<span style="color: #e6c07b; line-height: 26px;">Solution</span>&nbsp;</span>{<br><br>&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span>&nbsp;ListNode&nbsp;<span style="color: #61aeee; line-height: 26px;">ReverseList</span><span style="line-height: 26px;">(ListNode&nbsp;head)</span>&nbsp;</span>{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;next&nbsp;=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;pre&nbsp;=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">while</span>&nbsp;(head&nbsp;!=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;保存要反转到头的那个节点</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next&nbsp;=&nbsp;head.next;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;要反转的那个节点指向已经反转的上一个节点(备注:第一次反转的时候会指向null)</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;head.next&nbsp;=&nbsp;pre;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;上一个已经反转到头部的节点</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre&nbsp;=&nbsp;head;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;一直向链表尾走</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;head&nbsp;=&nbsp;next;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">return</span>&nbsp;pre;<br>&nbsp;&nbsp;}<br><br>}<br></code></pre> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">测试方法:</p> <pre><code style="overflow-x: auto; padding: 16px; color: rgb(171, 178, 191); background: rgb(40, 44, 52); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; border-radius: 0px;">&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span>&nbsp;<span style="color: #c678dd; line-height: 26px;">static</span>&nbsp;<span style="color: #c678dd; line-height: 26px;">void</span>&nbsp;<span style="color: #61aeee; line-height: 26px;">main</span><span style="line-height: 26px;">(String[]&nbsp;args)</span>&nbsp;</span>{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;a&nbsp;=&nbsp;<span style="color: #c678dd; line-height: 26px;">new</span>&nbsp;ListNode(<span style="color: #d19a66; line-height: 26px;">1</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;b&nbsp;=&nbsp;<span style="color: #c678dd; line-height: 26px;">new</span>&nbsp;ListNode(<span style="color: #d19a66; line-height: 26px;">2</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;c&nbsp;=&nbsp;<span style="color: #c678dd; line-height: 26px;">new</span>&nbsp;ListNode(<span style="color: #d19a66; line-height: 26px;">3</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;d&nbsp;=&nbsp;<span style="color: #c678dd; line-height: 26px;">new</span>&nbsp;ListNode(<span style="color: #d19a66; line-height: 26px;">4</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;e&nbsp;=&nbsp;<span style="color: #c678dd; line-height: 26px;">new</span>&nbsp;ListNode(<span style="color: #d19a66; line-height: 26px;">5</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;a.next&nbsp;=&nbsp;b;<br>&nbsp;&nbsp;&nbsp;&nbsp;b.next&nbsp;=&nbsp;c;<br>&nbsp;&nbsp;&nbsp;&nbsp;c.next&nbsp;=&nbsp;d;<br>&nbsp;&nbsp;&nbsp;&nbsp;d.next&nbsp;=&nbsp;e;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">new</span>&nbsp;Solution().ReverseList(a);<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">while</span>&nbsp;(e&nbsp;!=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(e.val);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e&nbsp;=&nbsp;e.next;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br></code></pre> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">输出:</p> <pre><code style="overflow-x: auto; padding: 16px; color: rgb(171, 178, 191); background: rgb(40, 44, 52); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; border-radius: 0px;">5<br>4<br>3<br>2<br>1<br></code></pre> <h2 style="font-weight: bold; font-size: 22px; margin-top: 20px; margin-right: 10px; margin-bottom: 0px;"><span style="font-size: 18px; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(145, 109, 213);">3. 链表中倒数第 k 个节点</span></h2> <h3 style="margin-top: 30px; margin-bottom: 15px; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #d89cf6;">题目描述</span></h3> <blockquote style="border-top: none; border-right: none; border-bottom: none; border-image: initial; font-size: 0.9em; overflow: auto; border-left-width: 3px; color: rgb(106, 115, 125); padding-top: 10px; padding-bottom: 10px; padding-left: 20px; margin-bottom: 20px; margin-top: 20px; border-left-color: rgb(216, 156, 246); background: rgb(244, 238, 255);"> <p style="padding-top: 8px; padding-bottom: 8px; font-size: 14px; word-spacing: 2px; margin-top: 0px; margin-bottom: 0px; color: black; line-height: 26px;">剑指 offer: 输入一个链表,输出该链表中倒数第 k 个结点。</p> </blockquote> <h3 style="margin-top: 30px; margin-bottom: 15px; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #d89cf6;">问题分析</span></h3> <blockquote style="border-top: none; border-right: none; border-bottom: none; border-image: initial; font-size: 0.9em; overflow: auto; border-left-width: 3px; color: rgb(106, 115, 125); padding-top: 10px; padding-bottom: 10px; padding-left: 20px; margin-bottom: 20px; margin-top: 20px; border-left-color: rgb(216, 156, 246); background: rgb(244, 238, 255);"> <p style="padding-top: 8px; padding-bottom: 8px; font-size: 14px; word-spacing: 2px; margin-top: 0px; margin-bottom: 0px; color: black; line-height: 26px;"><strong style="color: rgb(145, 109, 213);">「链表中倒数第 k 个节点也就是正数第(L-K+1)个节点,知道了只一点,这一题基本就没问题!」</strong></p> </blockquote> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">首先两个节点/指针,一个节点 node1 先开始跑,指针 node1 跑到 k-1 个节点后,另一个节点 node2 开始跑,当 node1 跑到最后时,node2 所指的节点就是倒数第 k 个节点也就是正数第(L-K+1)个节点。</p> <h3 style="margin-top: 30px; margin-bottom: 15px; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #d89cf6;">Solution</span></h3> <pre><code style="overflow-x: auto; padding: 16px; color: rgb(171, 178, 191); background: rgb(40, 44, 52); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; border-radius: 0px;"><span style="color: #5c6370; font-style: italic; line-height: 26px;">/*<br>public&nbsp;class&nbsp;ListNode&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;val;<br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;next&nbsp;=&nbsp;null;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode(int&nbsp;val)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.val&nbsp;=&nbsp;val;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}*/</span><br><br><span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;时间复杂度O(n),一次遍历即可</span><br><span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a?tpId=13&amp;tqId=11167&amp;tPage=1&amp;rp=1&amp;ru=/ta/coding-interviews&amp;qru=/ta/coding-interviews/question-ranking</span><br><span style="color: #c678dd; line-height: 26px;">public</span>&nbsp;<span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span>&nbsp;<span style="color: #e6c07b; line-height: 26px;">Solution</span>&nbsp;</span>{<br>&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span>&nbsp;ListNode&nbsp;<span style="color: #61aeee; line-height: 26px;">FindKthToTail</span><span style="line-height: 26px;">(ListNode&nbsp;head,&nbsp;<span style="color: #c678dd; line-height: 26px;">int</span>&nbsp;k)</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;如果链表为空或者k小于等于0</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">if</span>&nbsp;(head&nbsp;==&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>&nbsp;||&nbsp;k&nbsp;&lt;=&nbsp;<span style="color: #d19a66; line-height: 26px;">0</span>)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">return</span>&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;声明两个指向头结点的节点</span><br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;node1&nbsp;=&nbsp;head,&nbsp;node2&nbsp;=&nbsp;head;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;记录节点的个数</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">int</span>&nbsp;count&nbsp;=&nbsp;<span style="color: #d19a66; line-height: 26px;">0</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;记录k值,后面要使用</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">int</span>&nbsp;index&nbsp;=&nbsp;k;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;p指针先跑,并且记录节点数,当node1节点跑了k-1个节点后,node2节点开始跑,</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;当node1节点跑到最后时,node2节点所指的节点就是倒数第k个节点</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">while</span>&nbsp;(node1&nbsp;!=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node1&nbsp;=&nbsp;node1.next;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count++;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">if</span>&nbsp;(k&nbsp;&lt;&nbsp;<span style="color: #d19a66; line-height: 26px;">1</span>)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node2&nbsp;=&nbsp;node2.next;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k--;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;如果节点个数小于所求的倒数第k个节点,则返回空</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">if</span>&nbsp;(count&nbsp;&lt;&nbsp;index)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">return</span>&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">return</span>&nbsp;node2;<br><br>&nbsp;&nbsp;}<br>}<br></code></pre> <h2 style="font-weight: bold; font-size: 22px; margin-top: 20px; margin-right: 10px; margin-bottom: 0px;"><span style="font-size: 18px; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(145, 109, 213);">4. 删除链表的倒数第 N 个节点</span></h2> <blockquote style="border-top: none; border-right: none; border-bottom: none; border-image: initial; font-size: 0.9em; overflow: auto; border-left-width: 3px; color: rgb(106, 115, 125); padding-top: 10px; padding-bottom: 10px; padding-left: 20px; margin-bottom: 20px; margin-top: 20px; border-left-color: rgb(216, 156, 246); background: rgb(244, 238, 255);"> <p style="padding-top: 8px; padding-bottom: 8px; font-size: 14px; word-spacing: 2px; margin-top: 0px; margin-bottom: 0px; color: black; line-height: 26px;">Leetcode:给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。</p> </blockquote> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;"><strong style="color: rgb(145, 109, 213);">「示例:」</strong></p> <pre><code style="overflow-x: auto; padding: 16px; color: rgb(171, 178, 191); background: rgb(40, 44, 52); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; border-radius: 0px;">给定一个链表:&nbsp;1-&gt;2-&gt;3-&gt;4-&gt;5,&nbsp;和&nbsp;n&nbsp;=&nbsp;2.<br><br>当删除了倒数第二个节点后,链表变为&nbsp;1-&gt;2-&gt;3-&gt;5.<br><br></code></pre> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;"><strong style="color: rgb(145, 109, 213);">「说明:」</strong></p> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">给定的 n 保证是有效的。</p> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;"><strong style="color: rgb(145, 109, 213);">「进阶:」</strong></p> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">你能尝试使用一趟扫描实现吗?</p> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">该题在 leetcode 上有详细解答,具体可参考 Leetcode.</p> <h3 style="margin-top: 30px; margin-bottom: 15px; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #d89cf6;">问题分析</span></h3> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">我们注意到这个问题可以容易地简化成另一个问题:删除从列表开头数起的第 (L - n + 1)个结点,其中 L 是列表的长度。只要我们找到列表的长度 L,这个问题就很容易解决。</p> <h3 style="margin-top: 30px; margin-bottom: 15px; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #d89cf6;">Solution</span></h3> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;"><strong style="color: rgb(145, 109, 213);">「两次遍历法」</strong></p> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">首先我们将添加一个 <strong style="color: rgb(145, 109, 213);">「哑结点」</strong> 作为辅助,该结点位于列表头部。哑结点用来简化某些极端情况,例如列表中只含有一个结点,或需要删除列表的头部。在第一次遍历中,我们找出列表的长度 L。然后设置一个指向哑结点的指针,并移动它遍历列表,直至它到达第 (L - n) 个结点那里。<strong style="color: rgb(145, 109, 213);">「我们把第 (L - n)个结点的 next 指针重新链接至第 (L - n + 2)个结点,完成这个算法。」</strong></p> <pre><code style="overflow-x: auto; padding: 16px; color: rgb(171, 178, 191); background: rgb(40, 44, 52); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; border-radius: 0px;"><span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br>&nbsp;*&nbsp;Definition&nbsp;for&nbsp;singly-linked&nbsp;list.<br>&nbsp;*&nbsp;public&nbsp;class&nbsp;ListNode&nbsp;{<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;val;<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;next;<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ListNode(int&nbsp;x)&nbsp;{&nbsp;val&nbsp;=&nbsp;x;&nbsp;}<br>&nbsp;*&nbsp;}<br>&nbsp;*/</span><br><span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/description/</span><br><span style="color: #c678dd; line-height: 26px;">public</span>&nbsp;<span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span>&nbsp;<span style="color: #e6c07b; line-height: 26px;">Solution</span>&nbsp;</span>{<br>&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span>&nbsp;ListNode&nbsp;<span style="color: #61aeee; line-height: 26px;">removeNthFromEnd</span><span style="line-height: 26px;">(ListNode&nbsp;head,&nbsp;<span style="color: #c678dd; line-height: 26px;">int</span>&nbsp;n)</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;哑结点,哑结点用来简化某些极端情况,例如列表中只含有一个结点,或需要删除列表的头部</span><br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;dummy&nbsp;=&nbsp;<span style="color: #c678dd; line-height: 26px;">new</span>&nbsp;ListNode(<span style="color: #d19a66; line-height: 26px;">0</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;哑结点指向头结点</span><br>&nbsp;&nbsp;&nbsp;&nbsp;dummy.next&nbsp;=&nbsp;head;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;保存链表长度</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">int</span>&nbsp;length&nbsp;=&nbsp;<span style="color: #d19a66; line-height: 26px;">0</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;len&nbsp;=&nbsp;head;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">while</span>&nbsp;(len&nbsp;!=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;length++;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len&nbsp;=&nbsp;len.next;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;length&nbsp;=&nbsp;length&nbsp;-&nbsp;n;<br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;target&nbsp;=&nbsp;dummy;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;找到&nbsp;L-n&nbsp;位置的节点</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">while</span>&nbsp;(length&nbsp;&gt;&nbsp;<span style="color: #d19a66; line-height: 26px;">0</span>)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;target&nbsp;=&nbsp;target.next;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;length--;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;把第&nbsp;(L&nbsp;-&nbsp;n)个结点的&nbsp;next&nbsp;指针重新链接至第&nbsp;(L&nbsp;-&nbsp;n&nbsp;+&nbsp;2)个结点</span><br>&nbsp;&nbsp;&nbsp;&nbsp;target.next&nbsp;=&nbsp;target.next.next;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">return</span>&nbsp;dummy.next;<br>&nbsp;&nbsp;}<br>}<br></code></pre> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;"><strong style="color: rgb(145, 109, 213);">「复杂度分析:」</strong></p> <ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; font-size: 15px; list-style-type: circle;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; color: rgb(1, 1, 1); font-size: 14px;"><strong style="color: rgb(145, 109, 213);">「时间复杂度 O(L)」</strong> :该算法对列表进行了两次遍历,首先计算了列表的长度 LL 其次找到第 (L - n)(L−n) 个结点。 操作执行了 2L-n2L−n 步,时间复杂度为 O(L)O(L)。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; color: rgb(1, 1, 1); font-size: 14px;"><strong style="color: rgb(145, 109, 213);">「空间复杂度 O(1)」</strong> :我们只用了常量级的额外空间。</section></li></ul> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;"><strong style="color: rgb(145, 109, 213);">「进阶——一次遍历法:」</strong></p> <blockquote style="border-top: none; border-right: none; border-bottom: none; border-image: initial; font-size: 0.9em; overflow: auto; border-left-width: 3px; color: rgb(106, 115, 125); padding-top: 10px; padding-bottom: 10px; padding-left: 20px; margin-bottom: 20px; margin-top: 20px; border-left-color: rgb(216, 156, 246); background: rgb(244, 238, 255);"> <p style="padding-top: 8px; padding-bottom: 8px; font-size: 14px; word-spacing: 2px; margin-top: 0px; margin-bottom: 0px; color: black; line-height: 26px;">链表中倒数第 N 个节点也就是正数第(L-N+1)个节点。</p> </blockquote> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">其实这种方法就和我们上面第四题找“链表中倒数第 k 个节点”所用的思想是一样的。<strong style="color: rgb(145, 109, 213);">「基本思路就是:」</strong> 定义两个节点 node1、node2;node1 节点先跑,node1 节点 跑到第 n+1 个节点的时候,node2 节点开始跑.当 node1 节点跑到最后一个节点时,node2 节点所在的位置就是第 (L-n ) 个节点(L 代表总链表长度,也就是倒数第 n+1 个节点)</p> <pre><code style="overflow-x: auto; padding: 16px; color: rgb(171, 178, 191); background: rgb(40, 44, 52); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; border-radius: 0px;"><span style="color: #5c6370; font-style: italic; line-height: 26px;">/**<br>&nbsp;*&nbsp;Definition&nbsp;for&nbsp;singly-linked&nbsp;list.<br>&nbsp;*&nbsp;public&nbsp;class&nbsp;ListNode&nbsp;{<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;val;<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;next;<br>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ListNode(int&nbsp;x)&nbsp;{&nbsp;val&nbsp;=&nbsp;x;&nbsp;}<br>&nbsp;*&nbsp;}<br>&nbsp;*/</span><br><span style="color: #c678dd; line-height: 26px;">public</span>&nbsp;<span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span>&nbsp;<span style="color: #e6c07b; line-height: 26px;">Solution</span>&nbsp;</span>{<br>&nbsp;&nbsp;<span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span>&nbsp;ListNode&nbsp;<span style="color: #61aeee; line-height: 26px;">removeNthFromEnd</span><span style="line-height: 26px;">(ListNode&nbsp;head,&nbsp;<span style="color: #c678dd; line-height: 26px;">int</span>&nbsp;n)</span>&nbsp;</span>{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;dummy&nbsp;=&nbsp;<span style="color: #c678dd; line-height: 26px;">new</span>&nbsp;ListNode(<span style="color: #d19a66; line-height: 26px;">0</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;dummy.next&nbsp;=&nbsp;head;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;声明两个指向头结点的节点</span><br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;node1&nbsp;=&nbsp;dummy,&nbsp;node2&nbsp;=&nbsp;dummy;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;node1&nbsp;节点先跑,node1节点&nbsp;跑到第&nbsp;n&nbsp;个节点的时候,node2&nbsp;节点开始跑</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #5c6370; font-style: italic; line-height: 26px;">//&nbsp;当node1&nbsp;节点跑到最后一个节点时,node2&nbsp;节点所在的位置就是第&nbsp;(L-n&nbsp;)&nbsp;个节点,也就是倒数第&nbsp;n+1(L代表总链表长度)</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">while</span>&nbsp;(node1&nbsp;!=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node1&nbsp;=&nbsp;node1.next;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">if</span>&nbsp;(n&nbsp;&lt;&nbsp;<span style="color: #d19a66; line-height: 26px;">1</span>&nbsp;&amp;&amp;&nbsp;node1&nbsp;!=&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node2&nbsp;=&nbsp;node2.next;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n--;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;node2.next&nbsp;=&nbsp;node2.next.next;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">return</span>&nbsp;dummy.next;<br><br>&nbsp;&nbsp;}<br>}<br></code></pre> <h2 style="font-weight: bold; font-size: 22px; margin-top: 20px; margin-right: 10px; margin-bottom: 0px;"><span style="font-size: 18px; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(145, 109, 213);">5. 合并两个排序的链表</span></h2> <h3 style="margin-top: 30px; margin-bottom: 15px; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #d89cf6;">题目描述</span></h3> <blockquote style="border-top: none; border-right: none; border-bottom: none; border-image: initial; font-size: 0.9em; overflow: auto; border-left-width: 3px; color: rgb(106, 115, 125); padding-top: 10px; padding-bottom: 10px; padding-left: 20px; margin-bottom: 20px; margin-top: 20px; border-left-color: rgb(216, 156, 246); background: rgb(244, 238, 255);"> <p style="padding-top: 8px; padding-bottom: 8px; font-size: 14px; word-spacing: 2px; margin-top: 0px; margin-bottom: 0px; color: black; line-height: 26px;">剑指 offer:输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。</p> </blockquote> <h3 style="margin-top: 30px; margin-bottom: 15px; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #d89cf6;">问题分析</span></h3> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">我们可以这样分析:</p> <ol style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; font-size: 15px;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; color: rgb(1, 1, 1); font-size: 14px;">假设我们有两个链表 A,B;</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; color: rgb(1, 1, 1); font-size: 14px;">A 的头节点 A1 的值与 B 的头结点 B1 的值比较,假设 A1 小,则 A1 为头节点;</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; color: rgb(1, 1, 1); font-size: 14px;">A2 再和 B1 比较,假设 B1 小,则,A1 指向 B1;</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; color: rgb(1, 1, 1); font-size: 14px;">A2 再和 B2 比较 就这样循环往复就行了,应该还算好理解。</section></li></ol> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;">考虑通过递归的方式实现!</p> <h3 style="margin-top: 30px; margin-bottom: 15px; font-size: 16px; font-weight: bold; text-align: center;"><span style="border-bottom: 2px solid #d89cf6;">Solution</span></h3> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; font-size: 14px; word-spacing: 2px;"><strong style="color: rgb(145, 109, 213);">「递归版本:」</strong></p> <pre><code style="overflow-x: auto; padding: 16px; color: rgb(171, 178, 191); background: rgb(40, 44, 52); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; border-radius: 0px;"><span style="color: #5c6370; font-style: italic; line-height: 26px;">/*<br>public&nbsp;class&nbsp;ListNode&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;val;<br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode&nbsp;next&nbsp;=&nbsp;null;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;ListNode(int&nbsp;val)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.val&nbsp;=&nbsp;val;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}*/</span><br><span style="color: #5c6370; font-style: italic; line-height: 26px;">//https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337?tpId=13&amp;tqId=11169&amp;tPage=1&amp;rp=1&amp;ru=/ta/coding-interviews&amp;qru=/ta/coding-interviews/question-ranking</span><br><span style="color: #c678dd; line-height: 26px;">public</span>&nbsp;<span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">class</span>&nbsp;<span style="color: #e6c07b; line-height: 26px;">Solution</span>&nbsp;</span>{<br><span style="line-height: 26px;"><span style="color: #c678dd; line-height: 26px;">public</span>&nbsp;ListNode&nbsp;<span style="color: #61aeee; line-height: 26px;">Merge</span><span style="line-height: 26px;">(ListNode&nbsp;list1,ListNode&nbsp;list2)</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">if</span>(list1&nbsp;==&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">return</span>&nbsp;list2;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">if</span>(list2&nbsp;==&nbsp;<span style="color: #c678dd; line-height: 26px;">null</span>){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">return</span>&nbsp;list1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">if</span>(list1.val&nbsp;&lt;=&nbsp;list2.val){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list1.next&nbsp;=&nbsp;Merge(list1.next,&nbsp;list2);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">return</span>&nbsp;list1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<span style="color: #c678dd; line-height: 26px;">else</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list2.next&nbsp;=&nbsp;Merge(list1,&nbsp;list2.next);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #c678dd; line-height: 26px;">return</span>&nbsp;list2;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;}<br>}<br></code></pre> </section>
收藏(1255)  分享
相关标签: 互联网 职场 网站建设 就业
0个回复
  • 消灭零回复