1

Can someone please explain how it is getting the correct output when we are not updating the "head"?

public ListNode deleteDuplicates(ListNode head) {
    ListNode slow = head;
    while (slow.next != null) {
        if (slow.val == slow.next.val) {
            slow.next = slow.next.next;
        }
        else {
            slow = slow.next;
        }
    }
    return head;
  • hi and welcome to StackOverflow! please edit your question to clarify your specific problem or add additional details to highlight exactly what you need. as it's currently written, it’s hard to tell exactly what you're asking. see the How to Ask page for help clarifying this question. – Foued MOUSSI Jan 25 '20 at 07:35

1 Answers1

1

Here the ListNode object reference is passed as a value to deleteDuplicates and the same reference is assigned from head to slow (slow = head;), so after this method is returned your ListNode object will hold the change

for more clarification please check this

Thank you.

Sriram Umapthy
  • 678
  • 8
  • 11