FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_104/LeetCode_83_104.java at master · feixiangcode/algorithm · GitHub
feixiangcode
/
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_104
/
LeetCode_83_104.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
54 lines (52 loc) · 1.38 KB
Breadcrumbs
algorithm
/
Week_01
/
id_104
/
LeetCode_83_104.java
Copy path
File metadata and controls
54 lines (52 loc) · 1.38 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
44
45
46
47
48
49
50
51
52
53
54
/**
* 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 遍历集合
Set
<
Integer
>
sets
=
new
HashSet
();
if
(
head
==
null
){
return
head
;
}
ListNode
cur
=
head
;
while
(
cur
.
next
!=
null
){
//将当前元素值 存在Set集合里 进行判重
sets
.
add
(
cur
.
val
);
//3 判断下个元素是否已经在map中存在 如果存在则删除此元素
if
(
sets
.
contains
(
cur
.
next
.
val
)){
cur
=
removeNextNode
(
cur
);
}
if
(
cur
==
null
){
//异常数据 跳出循环
break
;
}
cur
=
cur
.
next
;
}
//4 打印最后的对象
ListNode
printNode
=
head
;
while
(
printNode
!=
null
){
System
.
out
.
println
(
printNode
.
val
);
printNode
=
printNode
.
next
;
}
return
head
;
}
/**
* 删除下一个元素
* node 当前元素
**/
private
ListNode
removeNextNode
(
ListNode
node
){
if
(
node
==
null
){
return
null
;
}
if
(
nodex
.
next
==
null
){
return
node
;
}
node
.
next
=
nodex
.
next
.
next
;
return
node
;
}
}
Back
|
FazBrowse Home
|
New Git URL