题目描述:给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。
原题介绍
示例一
digraph leetcode_92_1 {
bgcolor="#f7f7f7"
rankdir=LR
subgraph cluster_name {
subgraph cluster_1 {
n0 [label="1" shape=circle,color=blue,fillcolor=white,style=filled]
n1 [label="2" shape=circle,color=blue,fillcolor=lightblue,style=filled]
n2 [label="3" shape=circle,color=blue,fillcolor=lightblue,style=filled]
n3 [label="4" shape=circle,color=blue,fillcolor=lightblue,style=filled]
n4 [label="5" shape=circle,color=blue,fillcolor=white,style=filled]
rank=same;
n0 -> n1 -> n2 -> n3 -> n4
style=invis
}
subgraph cluster_2 {
r0 [label="1" shape=circle,color=blue,fillcolor=white,style=filled]
r1 [label="4" shape=circle,color=blue,fillcolor=lightblue,style=filled]
r2 [label="3" shape=circle,color=blue,fillcolor=lightblue,style=filled]
r3 [label="2" shape=circle,color=blue,fillcolor=lightblue,style=filled]
r4 [label="5" shape=circle,color=blue,fillcolor=white,style=filled]
rank=same;
r0 -> r1 -> r2 -> r3 -> r4
style=invis
}
n2 -> r2[constraint=false,color="red",pad=30]
}
label=<<B>图 1.1 算法示意图</B>>
}
1 | 输入:head = [1,2,3,4,5], left = 2, right = 4 |
示例二
1 | 输入:head = [5], left = 1, right = 1 |
解题思路
遍历拆分链表
如下图所示,通过遍历以 [left,right]
作为分界,把链表分为三个字表。
digraph leetcode_92_1 {
bgcolor="#f7f7f7"
rankdir=LR
subgraph cluster_name {
subgraph cluster_1 {
n0 [label="1" shape=circle,color=blue,fillcolor=white,style=filled,width=0.8,fixedsize=true]
n1 [label="..." shape=circle,color=blue,fillcolor=white,style=filled,width=0.8,fixedsize=true]
n2 [label="left-1" shape=circle,color=blue,fillcolor=white,style=filled,width=0.8,fixedsize=true]
n3 [label="left" shape=circle,color=red,fillcolor=lightblue,style=filled,width=0.8,fixedsize=true]
n4 [label="..." shape=circle,color=blue,fillcolor=lightblue,style=filled,width=0.8,fixedsize=true]
n5 [label="right" shape=circle,color=red,fillcolor=lightblue,style=filled,width=0.8,fixedsize=true]
n6 [label="right+1" shape=circle,color=blue,fillcolor=white,style=filled,width=0.8,fixedsize=true]
n7 [label="..." shape=circle,color=blue,fillcolor=white,style=filled,width=0.8,fixedsize=true]
n8 [label="n" shape=circle,color=blue,fillcolor=white,style=filled,width=0.8,fixedsize=true]
rank=same;
n0 -> n1 -> n2
n2 -> n3[style=dashed,color=red]
n3 -> n4 -> n5
n5 -> n6[style=dashed,color=red]
n6 -> n7 -> n8
style=invis
}
}
label=<<B>图 2.1 遍历拆分链表</B>>
}
分别处理三个子表
- 记录左边链表的head与end,记录右边链表的head。
- 遍历中间链表存入到栈当中,然后依次弹出栈中节点并链接为一个新的链表,根据栈后进先出的特性,新链表必定顺序是反过来的。
- 然后再依次按照左中右顺序拼接合并三个链表,得到最终结果。
digraph leetcode_92_1 {
bgcolor="#f7f7f7"
rankdir=LR
subgraph cluster_name {
subgraph cluster_1 {
n0 [label="1" shape=circle,color=blue,fillcolor=white,style=filled,width=0.8,fixedsize=true]
n1 [label="..." shape=circle,color=blue,fillcolor=white,style=filled,width=0.8,fixedsize=true]
n2 [label="left-1" shape=circle,color=blue,fillcolor=white,style=filled,width=0.8,fixedsize=true]
n3 [label="right" shape=circle,color=red,fillcolor=lightblue,style=filled,width=0.8,fixedsize=true]
n4 [label="..." shape=circle,color=blue,fillcolor=lightblue,style=filled,width=0.8,fixedsize=true]
n5 [label="left" shape=circle,color=red,fillcolor=lightblue,style=filled,width=0.8,fixedsize=true]
n6 [label="right+1" shape=circle,color=blue,fillcolor=white,style=filled,width=0.8,fixedsize=true]
n7 [label="..." shape=circle,color=blue,fillcolor=white,style=filled,width=0.8,fixedsize=true]
n8 [label="n" shape=circle,color=blue,fillcolor=white,style=filled,width=0.8,fixedsize=true]
rank=same;
n0 -> n1 -> n2
n2 -> n3[style=dashed,color=red]
n3 -> n4 -> n5
n5 -> n6[style=dashed,color=red]
n6 -> n7 -> n8
style=invis
}
}
label=<<B>图 2.2 最终结果</B>>
}
代码实现
栈实现
1 | public class Solution { |
递归实现
1 | class Solution { |