-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreverseKGroup.java
More file actions
executable file
·64 lines (57 loc) · 1.55 KB
/
reverseKGroup.java
File metadata and controls
executable file
·64 lines (57 loc) · 1.55 KB
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
/*
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
*/
import java.util.*;
class ListNode{
int val;
ListNode next;
ListNode(int x){
val = x;
next = null;
}
}
public class reverseKGroup{
public static void main(String[] args) {
ListNode head = new ListNode(0);
head.next = new ListNode(1);
head.next.next = new ListNode(2);
head.next.next.next = new ListNode(3);
ListNode rg = reverseK(head,2);
while(rg != null){
System.out.println(rg.val);
rg= rg.next;
}
}
public static ListNode reverseK(ListNode head, int k){
if(k == 1 || k == 0)
return head;
ListNode pre = new ListNode(0);
pre.next = head;
ListNode dump = pre;
int count = 0;
while(head != null){
count ++;
if(count%k == 0){
ListNode tail = head.next;
head = pre.next;
ListNode pn = head.next;
while(pn != tail){
head.next = pn.next;
pn.next = pre.next;
pre.next = pn;
pn = head.next;
}
pre = head;
}
head = head.next;
}
return dump.next;
}
}