Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
醒狮指南
JavaGuide
提交
36196cec
J
JavaGuide
项目概览
醒狮指南
/
JavaGuide
与 Fork 源项目一致
从无法访问的项目Fork
通知
5
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
JavaGuide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
36196cec
编写于
9月 20, 2018
作者:
S
SnailClimb
提交者:
GitHub
9月 20, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加目录
上级
34815463
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
131 addition
and
107 deletion
+131
-107
数据结构与算法/Leetcode-LinkList1.md
数据结构与算法/Leetcode-LinkList1.md
+131
-107
未找到文件。
数据结构与算法/Leetcode-LinkList1.md
浏览文件 @
36196cec
<!-- MarkdownTOC -->
-
[
1. 两数相加
](
#1-两数相加
)
-
[
题目描述
](
#题目描述
)
-
[
问题分析
](
#问题分析
)
-
[
Solution
](
#solution
)
-
[
2. 翻转链表
](
#2-翻转链表
)
-
[
题目描述
](
#题目描述-1
)
-
[
问题分析
](
#问题分析-1
)
-
[
Solution
](
#solution-1
)
-
[
3. 链表中倒数第k个节点
](
#3-链表中倒数第k个节点
)
-
[
题目描述
](
#题目描述-2
)
-
[
问题分析
](
#问题分析-2
)
-
[
Solution
](
#solution-2
)
-
[
4. 删除链表的倒数第N个节点
](
#4-删除链表的倒数第n个节点
)
-
[
问题分析
](
#问题分析-3
)
-
[
Solution
](
#solution-3
)
-
[
5. 合并两个排序的链表
](
#5-合并两个排序的链表
)
-
[
题目描述
](
#题目描述-3
)
-
[
问题分析
](
#问题分析-4
)
-
[
Solution
](
#solution-4
)
<!-- /MarkdownTOC -->
# 1. 两数相加
...
...
@@ -85,12 +109,12 @@ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
```
java
public
class
ListNode
{
int
val
;
ListNode
next
=
null
;
int
val
;
ListNode
next
=
null
;
ListNode
(
int
val
)
{
this
.
val
=
val
;
}
ListNode
(
int
val
)
{
this
.
val
=
val
;
}
}
```
...
...
@@ -103,23 +127,23 @@ public class ListNode {
*/
public
class
Solution
{
public
ListNode
ReverseList
(
ListNode
head
)
{
ListNode
next
=
null
;
ListNode
pre
=
null
;
public
ListNode
ReverseList
(
ListNode
head
)
{
while
(
head
!=
null
)
{
// 保存要反转到头的那个节点
next
=
head
.
next
;
// 要反转的那个节点指向已经反转的上一个节点(备注:第一次反转的时候会指向null)
head
.
next
=
pre
;
// 上一个已经反转到头部的节点
pre
=
head
;
// 一直向链表尾走
head
=
next
;
}
return
pre
;
}
ListNode
next
=
null
;
ListNode
pre
=
null
;
while
(
head
!=
null
)
{
// 保存要反转到头的那个节点
next
=
head
.
next
;
// 要反转的那个节点指向已经反转的上一个节点(备注:第一次反转的时候会指向null)
head
.
next
=
pre
;
// 上一个已经反转到头部的节点
pre
=
head
;
// 一直向链表尾走
head
=
next
;
}
return
pre
;
}
}
```
...
...
@@ -127,23 +151,23 @@ public class Solution {
测试方法:
```
java
public
static
void
main
(
String
[]
args
)
{
ListNode
a
=
new
ListNode
(
1
);
ListNode
b
=
new
ListNode
(
2
);
ListNode
c
=
new
ListNode
(
3
);
ListNode
d
=
new
ListNode
(
4
);
ListNode
e
=
new
ListNode
(
5
);
a
.
next
=
b
;
b
.
next
=
c
;
c
.
next
=
d
;
d
.
next
=
e
;
new
Solution
().
ReverseList
(
a
);
while
(
e
!=
null
)
{
System
.
out
.
println
(
e
.
val
);
e
=
e
.
next
;
}
}
public
static
void
main
(
String
[]
args
)
{
ListNode
a
=
new
ListNode
(
1
);
ListNode
b
=
new
ListNode
(
2
);
ListNode
c
=
new
ListNode
(
3
);
ListNode
d
=
new
ListNode
(
4
);
ListNode
e
=
new
ListNode
(
5
);
a
.
next
=
b
;
b
.
next
=
c
;
c
.
next
=
d
;
d
.
next
=
e
;
new
Solution
().
ReverseList
(
a
);
while
(
e
!=
null
)
{
System
.
out
.
println
(
e
.
val
);
e
=
e
.
next
;
}
}
```
输出:
...
...
@@ -185,33 +209,33 @@ public class ListNode {
// 时间复杂度O(n),一次遍历即可
// https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a?tpId=13&tqId=11167&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
public
class
Solution
{
public
ListNode
FindKthToTail
(
ListNode
head
,
int
k
)
{
// 如果链表为空或者k小于等于0
if
(
head
==
null
||
k
<=
0
)
{
return
null
;
}
// 声明两个指向头结点的节点
ListNode
node1
=
head
,
node2
=
head
;
// 记录节点的个数
int
count
=
0
;
// 记录k值,后面要使用
int
index
=
k
;
// p指针先跑,并且记录节点数,当node1节点跑了k-1个节点后,node2节点开始跑,
// 当node1节点跑到最后时,node2节点所指的节点就是倒数第k个节点
while
(
node1
!=
null
)
{
node1
=
node1
.
next
;
count
++;
if
(
k
<
1
&&
node1
!=
null
)
{
node2
=
node2
.
next
;
}
k
--;
}
// 如果节点个数小于所求的倒数第k个节点,则返回空
if
(
count
<
index
)
return
null
;
return
node2
;
}
public
ListNode
FindKthToTail
(
ListNode
head
,
int
k
)
{
// 如果链表为空或者k小于等于0
if
(
head
==
null
||
k
<=
0
)
{
return
null
;
}
// 声明两个指向头结点的节点
ListNode
node1
=
head
,
node2
=
head
;
// 记录节点的个数
int
count
=
0
;
// 记录k值,后面要使用
int
index
=
k
;
// p指针先跑,并且记录节点数,当node1节点跑了k-1个节点后,node2节点开始跑,
// 当node1节点跑到最后时,node2节点所指的节点就是倒数第k个节点
while
(
node1
!=
null
)
{
node1
=
node1
.
next
;
count
++;
if
(
k
<
1
&&
node1
!=
null
)
{
node2
=
node2
.
next
;
}
k
--;
}
// 如果节点个数小于所求的倒数第k个节点,则返回空
if
(
count
<
index
)
return
null
;
return
node2
;
}
}
```
...
...
@@ -264,29 +288,29 @@ public class Solution {
*/
// https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/description/
public
class
Solution
{
public
ListNode
removeNthFromEnd
(
ListNode
head
,
int
n
)
{
// 哑结点,哑结点用来简化某些极端情况,例如列表中只含有一个结点,或需要删除列表的头部
ListNode
dummy
=
new
ListNode
(
0
);
// 哑结点指向头结点
dummy
.
next
=
head
;
// 保存链表长度
int
length
=
0
;
ListNode
len
=
head
;
while
(
len
!=
null
)
{
length
++;
len
=
len
.
next
;
}
length
=
length
-
n
;
ListNode
target
=
dummy
;
// 找到 L-n 位置的节点
while
(
length
>
0
)
{
target
=
target
.
next
;
length
--;
}
// 把第 (L - n)个结点的 next 指针重新链接至第 (L - n + 2)个结点
target
.
next
=
target
.
next
.
next
;
return
dummy
.
next
;
}
public
ListNode
removeNthFromEnd
(
ListNode
head
,
int
n
)
{
// 哑结点,哑结点用来简化某些极端情况,例如列表中只含有一个结点,或需要删除列表的头部
ListNode
dummy
=
new
ListNode
(
0
);
// 哑结点指向头结点
dummy
.
next
=
head
;
// 保存链表长度
int
length
=
0
;
ListNode
len
=
head
;
while
(
len
!=
null
)
{
length
++;
len
=
len
.
next
;
}
length
=
length
-
n
;
ListNode
target
=
dummy
;
// 找到 L-n 位置的节点
while
(
length
>
0
)
{
target
=
target
.
next
;
length
--;
}
// 把第 (L - n)个结点的 next 指针重新链接至第 (L - n + 2)个结点
target
.
next
=
target
.
next
.
next
;
return
dummy
.
next
;
}
}
```
...
...
@@ -314,28 +338,28 @@ public class Solution {
* }
*/
public
class
Solution
{
public
ListNode
removeNthFromEnd
(
ListNode
head
,
int
n
)
{
ListNode
dummy
=
new
ListNode
(
0
);
dummy
.
next
=
head
;
// 声明两个指向头结点的节点
ListNode
node1
=
dummy
,
node2
=
dummy
;
// node1 节点先跑,node1节点 跑到第 n 个节点的时候,node2 节点开始跑
// 当node1 节点跑到最后一个节点时,node2 节点所在的位置就是第 (L-n ) 个节点,也就是倒数第 n+1(L代表总链表长度)
while
(
node1
!=
null
)
{
node1
=
node1
.
next
;
if
(
n
<
1
&&
node1
!=
null
)
{
node2
=
node2
.
next
;
}
n
--;
}
public
ListNode
removeNthFromEnd
(
ListNode
head
,
int
n
)
{
ListNode
dummy
=
new
ListNode
(
0
);
dummy
.
next
=
head
;
// 声明两个指向头结点的节点
ListNode
node1
=
dummy
,
node2
=
dummy
;
// node1 节点先跑,node1节点 跑到第 n 个节点的时候,node2 节点开始跑
// 当node1 节点跑到最后一个节点时,node2 节点所在的位置就是第 (L-n ) 个节点,也就是倒数第 n+1(L代表总链表长度)
while
(
node1
!=
null
)
{
node1
=
node1
.
next
;
if
(
n
<
1
&&
node1
!=
null
)
{
node2
=
node2
.
next
;
}
n
--;
}
node2
.
next
=
node2
.
next
.
next
;
node2
.
next
=
node2
.
next
.
next
;
return
dummy
.
next
;
return
dummy
.
next
;
}
}
}
```
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录