FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc206.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
/
lc206.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
39 lines (37 loc) · 1.11 KB
Breadcrumbs
leetcode
/
code
/
lc206.java
Copy path
File metadata and controls
39 lines (37 loc) · 1.11 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
package
code
;
/*
* 206. Reverse Linked List
* 题意:链表反转
* 难度:Easy
* 分类:Linked List
* 思路:2中方法:设置一个快走一步的快指针,注意赋值操作顺序。还有一种递归的方法。
* Tips:递归的方法有点绕,多看下
* lc25, lc206
*/
public
class
lc206
{
public
class
ListNode
{
int
val
;
ListNode
next
;
ListNode
(
int
x
) {
val
=
x
; }
}
public
ListNode
reverseList
(
ListNode
head
) {
ListNode
pre
=
null
;
//头结点变尾节点,指向null
while
(
head
!=
null
){
ListNode
next
=
head
.
next
;
head
.
next
=
pre
;
pre
=
head
;
head
=
next
;
}
return
pre
;
}
public
ListNode
reverseList2
(
ListNode
head
) {
//递归
return
reverseListInt
(
head
,
null
);
}
private
ListNode
reverseListInt
(
ListNode
head
,
ListNode
pre
) {
if
(
head
==
null
)
return
pre
;
ListNode
next
=
head
.
next
;
head
.
next
=
pre
;
return
reverseListInt
(
next
,
head
);
//尾递归,操作已经完成,最后返回最后结果罢了
}
}
Back
|
FazBrowse Home
|
New Git URL