FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/Intersection_of_two_linked_lists.java at master · tinycedar/leetcode · GitHub
tinycedar
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
5
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
Intersection_of_two_linked_lists.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
68 lines (56 loc) · 1.71 KB
Breadcrumbs
leetcode
/
Intersection_of_two_linked_lists.java
Copy path
File metadata and controls
68 lines (56 loc) · 1.71 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
55
56
57
58
59
60
61
62
63
64
65
66
package
com
.
netease
.
kaola
.
act
.
compose
;
public
class
Intersection_of_two_linked_lists
{
public
static
class
ListNode
{
int
val
;
ListNode
next
;
ListNode
(
int
x
) {
val
=
x
;
next
=
null
;
}
ListNode
(
int
x
,
ListNode
next
) {
this
.
val
=
x
;
this
.
next
=
next
;
}
}
public
ListNode
getIntersectionNode
(
ListNode
headA
,
ListNode
headB
) {
if
(
headA
==
null
||
headB
==
null
) {
return
null
;
}
int
sizeA
=
1
;
ListNode
tailA
=
headA
;
while
(
tailA
!=
null
&&
tailA
.
next
!=
null
) {
sizeA
++;
tailA
=
tailA
.
next
;
}
int
sizeB
=
1
;
ListNode
tailB
=
headB
;
while
(
tailB
!=
null
&&
tailB
.
next
!=
null
) {
sizeB
++;
tailB
=
tailB
.
next
;
}
if
(
tailA
.
val
!=
tailB
.
val
) {
return
null
;
}
int
diff
=
sizeA
-
sizeB
;
ListNode
startA
=
diff
>
0
?
headA
:
headB
;
for
(
int
i
=
Math
.
abs
(
diff
);
i
>
0
;
i
--) {
startA
=
startA
.
next
;
}
ListNode
startB
=
diff
>
0
?
headB
:
headA
;
while
(
startA
!=
null
&&
startB
!=
null
) {
if
(
startA
.
val
==
startB
.
val
) {
return
startA
;
}
startA
=
startA
.
next
;
startB
=
startB
.
next
;
}
return
null
;
}
public
static
void
main
(
String
[]
args
) {
Intersection_of_two_linked_lists
i
=
new
Intersection_of_two_linked_lists
();
ListNode
a
=
new
ListNode
(
3
,
null
);
ListNode
b
=
new
ListNode
(
2
,
new
ListNode
(
3
,
null
));
ListNode
result
=
i
.
getIntersectionNode
(
a
,
b
);
System
.
err
.
println
(
result
);
}
}
Back
|
FazBrowse Home
|
New Git URL