File tree 1 file changed +48
-9
lines changed
1 file changed +48
-9
lines changed Original file line number Diff line number Diff line change @@ -80,23 +80,22 @@ class Solution {
80
80
if (ss_prev== null )
81
81
break ;
82
82
ss= ss_prev. next;
83
- ss_prev. next = null ;
83
+ ss_prev. next = null ;
84
84
ListNode [] arr = reverse(ff);
85
85
if (ff_prev== null ){
86
86
head= arr[0 ];
87
- }
88
- else {
87
+ }else {
89
88
ff_prev. next = arr[0 ];
90
89
}
91
90
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;
95
94
}
96
95
return head;
97
- }
98
-
99
- private ListNode [] reverse (ListNode head ){
96
+ }
97
+
98
+ private ListNode [] reverse (ListNode head ){
100
99
ListNode pre = head;
101
100
ListNode now = head;
102
101
while (now!= null ){
@@ -110,6 +109,46 @@ class Solution {
110
109
}
111
110
```
112
111
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
+
113
152
### Discuss
114
153
115
154
## Review
You can’t perform that action at this time.
0 commit comments