Open mishin opened 1 year ago
package com.example.elastic;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class SolutionTest {
@Test
public void testDeleteMiddle() {
// Test case 1: List with odd number of nodes
ListNode head1 = new ListNode(1);
head1.next = new ListNode(2);
head1.next.next = new ListNode(3);
head1.next.next.next = new ListNode(4);
head1.next.next.next.next = new ListNode(5);
Solution solution = new Solution();
ListNode result1 = solution.deleteMiddle(head1);
Assertions.assertEquals("1 -> 2 -> 4 -> 5 -> null", getListAsString(result1));
// Test case 2: List with even number of nodes
ListNode head2 = new ListNode(1);
head2.next = new ListNode(2);
head2.next.next = new ListNode(3);
head2.next.next.next = new ListNode(4);
ListNode result2 = solution.deleteMiddle(head2);
Assertions.assertEquals("1 -> 2 -> 3 -> 4 -> null", getListAsString(result2));
// Test case 3: List with only one node
ListNode head3 = new ListNode(1);
ListNode result3 = solution.deleteMiddle(head3);
Assertions.assertNull(result3);
// Test case 4: Empty list
ListNode head4 = null;
ListNode result4 = solution.deleteMiddle(head4);
Assertions.assertNull(result4);
}
private String getListAsString(ListNode head) {
StringBuilder sb = new StringBuilder();
ListNode current = head;
while (current != null) {
sb.append(current.val);
if (current.next != null) {
sb.append(" -> ");
}
current = current.next;
}
sb.append(" -> null");
return sb.toString();
}
}