I am confused about the last "return" statement in the Java implementation of linked list below.
here is the code:
//remove the node with duplicate data and return the linked list
static Node removeDuplicates(Node head) {
if(head == null)
return head;
Node temp = head;
while(null != temp.next) {
if(temp.data == temp.next.data)
temp.next = temp.next.next;
else
temp = temp.next;
}
return head;
}
The last "return" statement is "return head", but shouldn't it be "return temp"?? My explanation would be that temp node is created to copy the head node, and traverse the entire linked list. At the end of this operation, it is the temp that is modified if there is any duplicate data, so the last statement should be "return temp".
The code above that I am confused about is actually correct, can anyone explain it to me?
Thank you!
A handle to a linked list is always the head of the list. The code doesn't reverse the list. It only removes duplicates. More precisely, if temp.data is identical to temp.next.data, then temp.next is being removed.
The head of the list is never changed in this process, therefore it is correct to return the original head, since it is also the head of the modified list.
For instance, suppose the original list was head --> a --> b --> c, where head,a,b,c all denote nodes in the list, and suppose the the value of a is identical to the values of b. To remove duplicates, we effectively change the "next" pointer in a to point to c instead of b. This yields the modified list head --> a --> c. The head node of this list is identical to the head node of the original list, and this is why we return head.
Related
Integer fun(Node *p) //Linked list
while(p not equals NULL)
print p-> data
p=p->next
End loop
Well, that's a common linked list function. Here is a common linked list architecture:
The idea is that you have blocks with two contents: the actual data and the pointer to the next object of the list.
In the posted code, p is the pointer to the first element of the list.
The intention is to print all the list. So, it follows the steps:
Print the first element's data.
Update p to point to the next element.
Print this element's data.
and so on until there are no more elements.
Given a linked list and a target value T, partition it such that all nodes less than T are listed before the nodes larger than or equal to target value T.
I found it is a little confused to me to understand codes.
while(head!=null){
if(head.value<target){
curSmall.next=head;
curSmall=curSmall.next;
} else{
curLarge.next=head;
curLarge=curLarge.next;
}
head=head.next;
}
curSmall.next=large.next;
curLarge.next=null;
return small.next;
I just don't understand these two parts large.next and return small.next?
In other word, when we connect the small and large linked list, the curSmall.next --> large.next, why we put large.next here, I am quite confused. And why we need to return small.next?
For example, after while loop of that code, two linked list are like below.
dummy_head(small)->a->c->d
dummy_head(large)->f->h
And curSmall and curLarge points the node 'd' and 'h' respectively.
Therefore you can get a desired linked list in the following steps.
The small linked list append the large linked list
=> curSmall.next = large.next;
The tail node 'h' of large linked list points to null => curLarge.next = null;
Return the head node 'a' of entire linked list => return small.next;
Exactly what the title says.
Also: What would list[0] yield (if one were to implement linked list indexing) if the linked list contained just a head pointing to null (or the programming language in question's functional equivalent of null).
Finally: Would the length be 0 or 1 (with length referring to the number of elements in the list) if the linked list contained only a head pointing to null?
void deleteDups(LinkedListNode n)
HashSet<Integer> set = new HashSet<Integer>();
LinkedListNode previous = null;
while(n!=null){
if(set.contains(n.data)){
previous.next = n.next;
}else {
set.add(n.data);
previous = n;
}
n = n.next;
}
}
This code snippet is to remove duplicate elements in a linked List.
I have been going through the linked list concepts in cracking the coding interview. Since only the code snippet is available I am not able to understand the flow and where the LinkedListNode n in the first line is actually from. I can understand that they are passing the entire linked list as a parameter, it would be helpful if anybody can tell me what the code for that LinkedListNode will be. Thank you in advance.
LinkedList traversal: Given the head (starting node) of the list, you could traverse the whole list. Each currentnode will have the information of nextnode which can be accessed by current.next.
In your code LinkedListNode 'n' is the starting node (head) of the list where duplicates needs to be removed
I had an interview yesterday. As it started, the first thing that the interviewer asked was
" In a doubly linked list, How many pointers will be affected on an insertion operation ? "
Since, he didn't specifically asked where to insert I replied that it depends on how many nodes are there in DLL.
As total pointers that will be affected will depend on whether the list is empty or not and where insertion takes place.
But, he didn't say anything whether I had convinced him or not.
Was I correct or maybe I missed something ?
I think the answer depends on whether we are inserting the new node in the middle of the list (surrounded by two nodes), or at the head or tail of the list.
For insertions in the middle of the list, to splice in a new node as follows:
A --- B
^^ splice M in here
A.next = M
M.prev = A
B.prev = M
M.next = B
Hence four pointer assignments take place. However, if the insertion be at the head or tail, then only two pointer assignments would be needed:
TAIL (insert M afterward)
TAIL.next = M
M.prev = TAIL