FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_118/leetcode_83_118.java at master · cloudrib/algorithm · GitHub
cloudrib
/
algorithm
Public
forked from
algorithm001/algorithm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_01
/
id_118
/
leetcode_83_118.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (39 loc) · 1.32 KB
Breadcrumbs
algorithm
/
Week_01
/
id_118
/
leetcode_83_118.java
Copy path
File metadata and controls
43 lines (39 loc) · 1.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
*
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class
Solution
{
public
ListNode
deleteDuplicates
(
ListNode
head
) {
// 1. 边界处理:链表为空或只有一个节点
if
(
head
==
null
||
head
.
next
==
null
){
return
head
;
}
// 2. 定义 left 和 right 两个前后指针。
ListNode
left
=
head
;
ListNode
right
=
head
.
next
;
// 3. 当 right 不为空时,进行如下操作:
// - 若两指针节点数据相同,则 right 移动,left 不动
// - 若两指针节点数据不同,则 left.next 指向 right,然后 left 和 right 都移动
while
(
right
!=
null
){
if
(
left
.
val
==
right
.
val
){
right
=
right
.
next
;
}
else
{
left
.
next
=
right
;
left
=
right
;
right
=
right
.
next
;
}
}
// 4. 边界处理:前面的循环终止条件是 right 为null,有两种情况:
// - left.next 为 null
// - left 及 到 null 前的节点数据相同
// so =>
left
.
next
=
null
;
return
head
;
}
}
Back
|
FazBrowse Home
|
New Git URL