-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLinkedListProcessor.java
66 lines (51 loc) · 1.9 KB
/
LinkedListProcessor.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package linked_list;
public class LinkedListProcessor {
static class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
}
}
public ListNode deleteMiddle(ListNode head) {
if (head == null || head.next == null) return null;
ListNode fast = head, slow = head, prev = null;
while (fast != null && fast.next != null) {
fast = fast.next.next;
prev = slow;
slow = slow.next;
}
prev.next = slow.next;
return head;
}
private static ListNode arrayToList(int[] arr) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
for (int num : arr) {
tail.next = new ListNode(num);
tail = tail.next;
}
return dummy.next;
}
private static boolean areListsEqual(ListNode l1, ListNode l2) {
while (l1 != null && l2 != null) {
if (l1.val != l2.val) return false;
l1 = l1.next;
l2 = l2.next;
}
return l1 == null && l2 == null;
}
public static void main(String[] args) {
LinkedListProcessor processor = new LinkedListProcessor();
ListNode head1 = arrayToList(new int[]{1,3,4,7,1,2,6});
ListNode expected1 = arrayToList(new int[]{1,3,4,1,2,6});
assert areListsEqual(processor.deleteMiddle(head1), expected1) : "Test case 1 failed";
ListNode head2 = arrayToList(new int[]{1,2,3,4});
ListNode expected2 = arrayToList(new int[]{1,2,4});
assert areListsEqual(processor.deleteMiddle(head2), expected2) : "Test case 2 failed";
ListNode head3 = arrayToList(new int[]{2,1});
ListNode expected3 = arrayToList(new int[]{2});
assert areListsEqual(processor.deleteMiddle(head3), expected3) : "Test case 3 failed";
System.out.println("All test cases passed!");
}
}