-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathKReverse.java
More file actions
43 lines (36 loc) · 866 Bytes
/
KReverse.java
File metadata and controls
43 lines (36 loc) · 866 Bytes
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
package LinkedLists;
/**
* Author - archit.s
* Date - 19/10/18
* Time - 9:11 PM
*/
public class KReverse {
public ListNode reverseList(ListNode A, int B) {
ListNode head = null;
ListNode last = null;
ListNode t = A;
while(t!=null){
int count = 1;
ListNode current = t;
ListNode prev = null;
ListNode next = null;
while(count<=B){
next = current.next;
current.next = prev;
prev = current;
current = next;
count++;
}
if(head == null){
head = prev;
last = t;
}
else{
last.next = prev;
last = t;
}
t = next;
}
return head;
}
}