Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
senthil kumar
Suggestions

senthil kumar

Mar 20, 2024

Kindly add the main method and print method to avoid confusion and add clarity.

public void printList(ListNode head) {
    ListNode current = head;
    while (current != null) {
        System.out.print(current.val + " ");
        current = current.next;
    }
    System.out.println();
}

public static void main(String[] args) {
    Solution solution = new Solution();
    
    // Test Example 1
    ListNode head1 = new ListNode(1, new ListNode(1, new ListNode(2)));
    ListNode result1 = solution.deleteDuplicates(head1); // Expected: 1 -> 2
    solution.printList(result1);

    // Test Example 2
    ListNode head2 = new ListNode(1, new ListNode(2, new ListNode(2, new ListNode(3))));
    ListNode result2 = solution.deleteDuplicates(head2); // Expected: 1 -> 2 -> 3
    solution.printList(result2);
    
    // Test Example 3
    ListNode head3 = new ListNode(3, new ListNode(3, new ListNode(3)));
    ListNode result3 = solution.deleteDuplicates(head3); // Expected: 3
    solution.printList(result3);
}

2

0

Comments
Comments

On this page