예제 #1
0
 @Test
 public void Test3() {
   int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
   ListNode head = ListNode.constructLinkedList(nums);
   ListNode actual = solution.swapPairs(head);
   int[] exps = {2, 1, 4, 3, 6, 5, 8, 7, 10, 9};
   ListNode expected = ListNode.constructLinkedList(exps);
   assertTrue(ListNode.isSameList(actual, expected));
 }
예제 #2
0
 @Test
 public void Test4() {
   ListNode h1 = ListNode.constructLinkedList(Arrays.asList(3));
   ListNode h2 = ListNode.constructLinkedList(Arrays.asList(1));
   ListNode[] lists = {h1, h2};
   ListNode actual = solution.mergeKLists(lists);
   ListNode expected = ListNode.constructLinkedList(Arrays.asList(1, 3));
   assertTrue(ListNode.isSameList(actual, expected));
 }
예제 #3
0
 @Test
 public void Test4() {
   int[] nums = {1};
   ListNode head = ListNode.constructLinkedList(nums);
   solution.reorderList(head);
   int[] exps = {1};
   ListNode expected = ListNode.constructLinkedList(exps);
   assertTrue(ListNode.isSameList(head, expected));
 }
예제 #4
0
 @Test
 public void Test3() {
   int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
   ListNode head = ListNode.constructLinkedList(nums);
   solution.reorderList(head);
   int[] exps = {1, 9, 2, 8, 3, 7, 4, 6, 5};
   ListNode expected = ListNode.constructLinkedList(exps);
   assertTrue(ListNode.isSameList(head, expected));
 }
예제 #5
0
 // 1 -> 3-> 5 \
 //           ==> 1->2->3->4->5
 // 2 -> 4 /
 @Test
 public void Test4() {
   int[] nums1 = {1, 3, 5};
   ListNode l1 = ListNode.constructLinkedList(nums1);
   int[] nums2 = {2, 4};
   ListNode l2 = ListNode.constructLinkedList(nums2);
   ListNode actual = solution.mergeTwoLists(l1, l2);
   int[] exps = {1, 2, 3, 4, 5};
   ListNode expected = ListNode.constructLinkedList(exps);
   assertTrue(ListNode.isSameList(actual, expected));
 }
예제 #6
0
 @Test
 public void Test1() {
   ListNode node11 = new ListNode(1);
   ListNode node21 = new ListNode(2);
   ListNode actual = solution.mergeTwoLists(node11, node21);
   ListNode node1 = new ListNode(1);
   ListNode node2 = new ListNode(2);
   node1.next = node2;
   ListNode expect = node1;
   assertTrue(ListNode.isSameList(actual, expect));
 }
예제 #7
0
 @Test
 public void Test0() {
   ListNode node11 = null;
   ListNode node21 = null;
   ListNode actual = solution.mergeTwoLists(node11, node21);
   ListNode expect = null;
   assertTrue(ListNode.isSameList(actual, expect));
 }
예제 #8
0
 public ListNode deleteDuplicates(ListNode head) {
   ListNode dummy = new ListNode(0);
   dummy.next = head;
   ListNode pre = dummy; // last unique node we found
   ListNode node = pre.next;
   while (node != null) {
     // find the last node that has the same value as current node
     while (node.next != null && node.next.val == node.val) {
       node = node.next;
     }
     // now, node has different value from its next node (if next
     // node exists), we need to compare with previous node to
     // see whether `node` has unique value
     if (pre.next == node) {
       // ! don't use (pre.val == node.val) unless you are sure
       // dummy has different value from head node
       pre = node;
     } else {
       pre.next = node.next;
     }
     node = node.next;
   }
   return dummy.next;
 }