Skip to content

Commit bcf25b2

Browse files
committed
note
1 parent e0551ea commit bcf25b2

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

guangxu/202101/20210117.md

+48-9
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,22 @@ class Solution {
8080
if(ss_prev==null)
8181
break;
8282
ss=ss_prev.next;
83-
ss_prev.next = null;
83+
ss_prev.next = null;
8484
ListNode[] arr = reverse(ff);
8585
if(ff_prev==null){
8686
head=arr[0];
87-
}
88-
else{
87+
}else{
8988
ff_prev.next = arr[0];
9089
}
9190
ff.next = ss;
92-
ss_prev = arr[1];
93-
ff_prev = ss_prev;
94-
ff=ss;
91+
ss_prev = arr[1];
92+
ff_prev = ss_prev;
93+
ff=ss;
9594
}
9695
return head;
97-
}
98-
99-
private ListNode[] reverse(ListNode head){
96+
}
97+
98+
private ListNode[] reverse(ListNode head){
10099
ListNode pre = head;
101100
ListNode now = head;
102101
while(now!=null){
@@ -110,6 +109,46 @@ class Solution {
110109
}
111110
```
112111

112+
113+
```java
114+
class Solution {
115+
public ListNode reverseKGroup(ListNode head, int k) {
116+
117+
//checking size
118+
ListNode sizeNode = head;
119+
int size = 0;
120+
while(sizeNode!=null){
121+
sizeNode= sizeNode.next;
122+
size++;
123+
}
124+
125+
if(head==null || head.next == null || size<k )
126+
return head;
127+
128+
ListNode curr = head;
129+
ListNode prev= null;
130+
ListNode nextNode ;
131+
int count = 0;
132+
133+
while(curr!=null && count < k) {
134+
nextNode= curr.next;
135+
curr.next = prev;
136+
prev = curr;
137+
curr = nextNode;
138+
count++;
139+
}
140+
141+
ListNode temp = prev;
142+
while(temp.next!=null){
143+
temp = temp.next;
144+
}
145+
temp.next = reverseKGroup(curr,k);
146+
return prev;
147+
148+
}
149+
}
150+
```
151+
113152
### Discuss
114153

115154
## Review

0 commit comments

Comments
 (0)