FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_101/LeetCode_25_101.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_101
/
LeetCode_25_101.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
58 lines (55 loc) · 1.53 KB
Breadcrumbs
algorithm
/
Week_01
/
id_101
/
LeetCode_25_101.java
Copy path
File metadata and controls
58 lines (55 loc) · 1.53 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
package
linkedlist
.
reverseNodesInkGroup
;
import
linkedlist
.
ListNode
;
/**
* @author Seina
* @version 2019-01-20 18:50:06
*/
public
class
ReverseNodesInkGroup
{
/**
*
* @param head 链表第一个结点
* @param k:是一个正整数,他的值小于或等于链表的长度
* @return 反转后的链表第一个结点
*/
public
ListNode
reverseKGroup
(
ListNode
head
,
int
k
) {
//k等于1,每1个结点进行翻转,相当于直接返回原始链表
if
(
k
<
2
) {
return
head
;
}
ListNode
start
;
ListNode
pre
=
null
;
ListNode
cur
=
head
;
ListNode
next
;
int
count
=
1
;
while
(
cur
!=
null
) {
next
=
cur
.
next
;
if
(
count
==
k
) {
start
=
pre
==
null
?
head
:
pre
.
next
;
//head用来追踪链表反转过程中的第一个结点
head
=
pre
==
null
?
cur
:
head
;
resign
(
pre
,
start
,
cur
,
next
);
pre
=
start
;
count
=
0
;
}
count
++;
cur
=
next
;
}
return
head
;
}
private
void
resign
(
ListNode
left
,
ListNode
start
,
ListNode
end
,
ListNode
right
) {
ListNode
p
=
start
;
ListNode
c
=
start
.
next
;
ListNode
n
;
while
(
c
!=
right
) {
n
=
c
.
next
;
c
.
next
=
p
;
p
=
c
;
c
=
n
;
}
if
(
left
!=
null
) {
left
.
next
=
end
;
}
start
.
next
=
right
;
}
}
Back
|
FazBrowse Home
|
New Git URL