-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseKList.java
More file actions
37 lines (26 loc) · 856 Bytes
/
reverseKList.java
File metadata and controls
37 lines (26 loc) · 856 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
/*Given a singly linked list and an integer K, reverses the nodes of the
list K at a time and returns modified linked list.
NOTE : The length of the list is divisible by K
Example :
Given linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 and K=2,
You should return 2 -> 1 -> 4 -> 3 -> 6 -> 5
Try to solve the problem using constant extra space.
*/
public ListNode reverseList(ListNode a, int b) {
if(a==null || a.next==null || b==1||b==0)
return a;
ListNode prev=null, curr =null, start=a;
int count =0;
ListNode next=null;
while(count<b && start!=null)
{
next = start.next;
start.next = prev;
prev = start;
start=next;
count++;
}
if(start!=null)
a.next= reverseList(start,b);
return prev;
}