forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseLists2.java
More file actions
44 lines (36 loc) · 841 Bytes
/
ReverseLists2.java
File metadata and controls
44 lines (36 loc) · 841 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
44
package LinkedLists;
/**
* Author - archit.s
* Date - 19/10/18
* Time - 10:46 AM
*/
public class ReverseLists2 {
public ListNode reverseBetween(ListNode A, int B, int C) {
int count = 1;
ListNode start = A;
ListNode firstPart = null;
while(count < B){
firstPart = start;
start = start.next;
count++;
}
ListNode current = start;
ListNode prev = null;
ListNode next = null;
while(count<=C){
next = current.next;
current.next = prev;
prev = current;
current = next;
count++;
}
start.next = next;
if(firstPart == null){
A = prev;
}
else{
firstPart.next = prev;
}
return A;
}
}