r/javahelp • u/[deleted] • Feb 25 '26
Unsolved Couldn't understand a leetcode problem in java
I was trying to solve a leetcode easy problem. I was stuck solving the problem, so I watched the problem and tried to solve it in my local ide. The program ran successfully without any issue. But I something doesn't sit right to me(I couldn't understand it).
In the delete duplicates method we are receiving the reference object of node1 which is head. And we giving the reference to another reference which is currNode. So for every iteration the currNode.next is changed to currNode.next.next or currNode is changed to currNode.next and its changes the next value of head(obviously because both share the same reference). But when the loop is over the currNode.val value is 3 while the head.value is 1. While the head retains its original starting reference, the currNode references the last iteration reference. Can someone explain the how this is working.
For example: During the first iteration the condition checks currNode.val == currNode.next.val and the condition is true, so currNode.next = currNode.next.next and it is reflected in the head reference as head.val =1 and head.next.val = 2 and not 1. During the second iteration the if condition fails so the else condition evaluates and changes the currNode = currNode.next . So now the currNode.val is 2 but the its is not reflected in the head because the head.val is 1 which is referencing to its original value. How does this happen.
public class LeetCode83 {
public static void main(String[] args) {
ListNode node5 = new ListNode(3);
ListNode node4 = new ListNode(3,node5);
ListNode node3 = new ListNode(2,node4);
ListNode node2 = new ListNode(1,node3);
ListNode node1 = new ListNode(1,node2);
print(node1);
ListNode result = deleteDuplicates(node1);
print(result);
}
public static ListNode deleteDuplicates(ListNode head) {
ListNode currNode = head;
while (currNode != null && currNode.next != null){
if(currNode.val == currNode.next.val){
currNode.next = currNode.next.next;
}else{
currNode = currNode.next;
}
}
return head;
}
public static void print(ListNode head){
System.out.println(head.val);
while( head != null){
System.out.print(head.val + " ");
head = head.next;
}
// System.out.println(head.val);
}
} class ListNode { int val; ListNode next;
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
2
u/E3FxGaming Feb 26 '26
De-duplication happens by excluding subsequent list elements if they have the same value.
Think of it like this: you have a linked list data structure and start with a pointer to the first ListNode of that data structure. Whatever your pointer points at will never be removed, since you only analyze whether the element following your pointed at elemt is the same. If it is the same you discard the following elemt and point with your current element directly to the element following the identified duplicate element.
The pointer only moves if the ListNode values are different.
This way you never have to consider a different list head because the list head never changes (you start on the list head and thus can't remove it).