FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc234.java at master · mJackie/leetcode · GitHub
mJackie
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
405
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
/
code
/
lc234.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
44 lines (43 loc) · 1.25 KB
Breadcrumbs
leetcode
/
code
/
lc234.java
Copy path
File metadata and controls
44 lines (43 loc) · 1.25 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
package
code
;
/*
* 234. Palindrome Linked List
* 题意:判断链表是否是回文的
* 难度:Easy
* 分类:LinkedList, Two Pointers
* 思路:反转一半就行了,避免了空间开销
* Tips:很好的题,考了 Two Pointers, 还考了链表反转
* lc5, lc9, lc125, lc131, lc234, lc647
*/
public
class
lc234
{
public
class
ListNode
{
int
val
;
ListNode
next
;
ListNode
(
int
x
) {
val
=
x
; }
}
public
boolean
isPalindrome
(
ListNode
head
) {
if
(
head
==
null
||
head
.
next
==
null
)
return
true
;
ListNode
slow
=
head
;
ListNode
fast
=
head
;
while
(
fast
.
next
!=
null
&&
fast
.
next
.
next
!=
null
) {
slow
=
slow
.
next
;
fast
=
fast
.
next
.
next
;
}
ListNode
head2
=
reverse
(
slow
.
next
,
null
);
slow
.
next
=
null
;
//注意截断一下
while
(
head2
!=
null
&&
head
!=
null
){
if
(
head2
.
val
!=
head
.
val
)
return
false
;
head
=
head
.
next
;
head2
=
head2
.
next
;
}
return
true
;
}
public
ListNode
reverse
(
ListNode
head
,
ListNode
pre
){
ListNode
next
=
head
.
next
;
head
.
next
=
pre
;
if
(
next
==
null
)
return
head
;
return
reverse
(
next
,
head
);
}
}
Back
|
FazBrowse Home
|
New Git URL