-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
41 lines (37 loc) · 1.26 KB
/
Main.java
File metadata and controls
41 lines (37 loc) · 1.26 KB
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
class ListNode {
String val;
ListNode next;
ListNode(String val) { this.val = val; }
}
public class Main {
public static ListNode reverseBetween(ListNode head, int left, int right) {
ListNode dummy = new ListNode(""), prev = dummy;
dummy.next = head;
for (int i = 1; i < left; i++) prev = prev.next;
ListNode curr = prev.next;
for (int i = 0; i < right - left; i++) {
ListNode tmp = curr.next;
curr.next = tmp.next;
tmp.next = prev.next;
prev.next = tmp;
}
return dummy.next;
}
public static void printList(ListNode head) {
while (head != null) {
System.out.print(head.val + (head.next != null ? " -> " : ""));
head = head.next;
}
}
public static ListNode arrayToList(String[] arr) {
ListNode dummy = new ListNode(""), curr = dummy;
for (String s : arr) curr = curr.next = new ListNode(s);
return dummy.next;
}
public static void main(String[] args) {
String[] arr = {"Bread", "Eggs", "Milk", "Cheese", "Tomatoes"};
ListNode head = arrayToList(arr);
head = reverseBetween(head, 2, 4);
printList(head);
}
}