Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
GreyZeng
algorithm
提交
ee330de5
A
algorithm
项目概览
GreyZeng
/
algorithm
通知
10
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ee330de5
编写于
9月 09, 2023
作者:
GreyZeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
leetcode 92 reverse between
上级
0a3ab14a
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
74 addition
and
127 deletion
+74
-127
src/main/java/list/LeetCode_0092_ReverseLinkedListII.java
src/main/java/list/LeetCode_0092_ReverseLinkedListII.java
+0
-127
src/main/java/resolved/list/LeetCode_0092_ReverseLinkedListII.java
...java/resolved/list/LeetCode_0092_ReverseLinkedListII.java
+74
-0
未找到文件。
src/main/java/list/LeetCode_0092_ReverseLinkedListII.java
已删除
100644 → 0
浏览文件 @
0a3ab14a
package
list
;
// 反转单链表一部分
// https://leetcode.cn/problems/reverse-linked-list-ii/
// 笔记:https://www.cnblogs.com/greyzeng/p/16629407.html
public
class
LeetCode_0092_ReverseLinkedListII
{
public
static
class
ListNode
{
int
val
;
ListNode
next
;
ListNode
()
{
}
ListNode
(
int
val
)
{
this
.
val
=
val
;
}
ListNode
(
int
val
,
ListNode
next
)
{
this
.
val
=
val
;
this
.
next
=
next
;
}
}
public
static
ListNode
reverseBetween
(
ListNode
head
,
int
m
,
int
n
)
{
if
(
m
==
n
)
{
// 不变
return
head
;
}
ListNode
startPre
=
null
;
ListNode
start
=
null
;
ListNode
end
;
ListNode
endAfter
=
null
;
ListNode
cur
=
head
;
int
i
=
1
;
while
(
i
<=
n
)
{
if
(
i
==
m
-
1
)
{
startPre
=
cur
;
}
else
if
(
i
==
m
)
{
start
=
cur
;
}
else
if
(
i
==
n
)
{
end
=
cur
;
if
(
end
.
next
!=
null
)
{
endAfter
=
end
.
next
;
end
.
next
=
null
;
}
}
cur
=
cur
.
next
;
i
++;
}
i
=
m
;
ListNode
pre
=
null
;
// 反转链表操作
while
(
i
<=
n
)
{
ListNode
tmp
=
start
.
next
;
start
.
next
=
pre
;
pre
=
start
;
start
=
tmp
;
if
(
i
==
m
)
{
pre
.
next
=
endAfter
;
}
i
++;
}
// 换头了
if
(
m
==
1
&&
n
!=
1
)
{
return
pre
;
}
startPre
.
next
=
pre
;
return
head
;
}
public
static
ListNode
reverseBetweenRecursive
(
ListNode
head
,
int
m
,
int
n
)
{
if
(
m
==
1
)
{
return
reverseN
(
head
,
n
);
}
head
.
next
=
reverseBetweenRecursive
(
head
.
next
,
m
-
1
,
n
-
1
);
return
head
;
}
static
ListNode
successor
=
null
;
// 反转链表前N个节点
public
static
ListNode
reverseN
(
ListNode
head
,
int
n
)
{
if
(
n
==
1
)
{
successor
=
head
.
next
;
return
head
;
}
ListNode
last
=
reverseN
(
head
.
next
,
n
-
1
);
head
.
next
.
next
=
head
;
head
.
next
=
successor
;
return
last
;
}
public
static
void
main
(
String
[]
args
)
{
testCase1
();
testCase2
();
}
public
static
void
testCase1
()
{
System
.
out
.
println
(
"begin test"
);
ListNode
head
=
new
ListNode
(
1
);
head
.
next
=
new
ListNode
(
2
);
head
.
next
.
next
=
new
ListNode
(
3
);
head
.
next
.
next
.
next
=
new
ListNode
(
4
);
head
.
next
.
next
.
next
.
next
=
new
ListNode
(
5
);
head
.
next
.
next
.
next
.
next
.
next
=
new
ListNode
(
6
);
ListNode
t
=
reverseBetween
(
head
,
2
,
4
);
while
(
t
!=
null
)
{
System
.
out
.
print
(
t
.
val
+
" "
);
t
=
t
.
next
;
}
System
.
out
.
println
(
"end test"
);
}
public
static
void
testCase2
()
{
System
.
out
.
println
(
"begin test"
);
ListNode
head
=
new
ListNode
(
3
);
head
.
next
=
new
ListNode
(
5
);
ListNode
t
=
reverseBetween
(
head
,
1
,
2
);
while
(
t
!=
null
)
{
System
.
out
.
print
(
t
.
val
+
" "
);
t
=
t
.
next
;
}
System
.
out
.
println
(
"end test"
);
}
}
src/main/java/resolved/list/LeetCode_0092_ReverseLinkedListII.java
0 → 100644
浏览文件 @
ee330de5
package
resolved.list
;
// 值得反复练习的习题
// 反转单链表一部分
// https://leetcode.cn/problems/reverse-linked-list-ii/
// 笔记:https://www.cnblogs.com/greyzeng/p/16629407.html
public
class
LeetCode_0092_ReverseLinkedListII
{
public
static
class
ListNode
{
int
val
;
ListNode
next
;
ListNode
()
{}
ListNode
(
int
val
)
{
this
.
val
=
val
;
}
ListNode
(
int
val
,
ListNode
next
)
{
this
.
val
=
val
;
this
.
next
=
next
;
}
}
public
ListNode
reverseBetween
(
ListNode
h
,
int
m
,
int
n
)
{
if
(
m
==
n
||
h
==
null
||
h
.
next
==
null
)
{
return
h
;
}
ListNode
dummy
=
new
ListNode
(
0
,
h
);
ListNode
startPre
=
dummy
;
ListNode
start
=
h
;
for
(
int
i
=
1
;
i
<
m
;
i
++)
{
startPre
=
startPre
.
next
;
start
=
start
.
next
;
}
ListNode
pre
=
null
;
ListNode
cur
=
start
;
for
(
int
i
=
m
;
i
<=
n
;
i
++)
{
ListNode
tmp
=
cur
.
next
;
cur
.
next
=
pre
;
pre
=
cur
;
cur
=
tmp
;
}
if
(
m
!=
1
)
{
startPre
.
next
=
pre
;
start
.
next
=
cur
;
return
h
;
}
else
{
start
.
next
=
cur
;
return
pre
;
}
}
public
static
ListNode
reverseBetweenRecursive
(
ListNode
head
,
int
m
,
int
n
)
{
if
(
m
==
1
)
{
return
reverseN
(
head
,
n
);
}
head
.
next
=
reverseBetweenRecursive
(
head
.
next
,
m
-
1
,
n
-
1
);
return
head
;
}
static
ListNode
successor
=
null
;
// 反转链表前N个节点
public
static
ListNode
reverseN
(
ListNode
head
,
int
n
)
{
if
(
n
==
1
)
{
successor
=
head
.
next
;
return
head
;
}
ListNode
last
=
reverseN
(
head
.
next
,
n
-
1
);
head
.
next
.
next
=
head
;
head
.
next
=
successor
;
return
last
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录