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
Related
Hi Everyone hope all are good in this COVID time
So In a standard, all levels view i am able to view the links but when i change the view to level1 the links are not displayed.How can i display it any idea,why i need is i dont need to much data as in all level just enough as in level 1 view.
Thanks in advance
It is clear that the links are not shown: after all, Object 644 "ABCD" is not linked to any other object and changing the view to level 1 does exactly this: it shows only objects of Level 1 and nothing else, there is no cumulation.
The solution for your problem depends on what you want to do concretely.
If your goal is to count the links in the chapter, you could probably write a Layout DXL column like this
void recursiveCountOutLinks(Object o, int &iCount) {
// first count the links going out of Object o
Link l
for l in o->"*" do iCount++
// next, add all Links on lower levels
Object oChild
for oChild in o do {
if isDeleted(oChild) then continue
recursiveCountOutLinks (oChild, iCount)
}
}
int iTotal = 0
if (level obj == 1) {
recursiveCountOutLinks(obj, iTotal)
display iTotal ""
}
If your goal is to export these links you will have to adopt your exporter by doing this recursively as shown above.
Thanks for reading my first question. I'm just starting out with google sheets to please bear with me.
Here is the sheet I'm working with
So,this sheet if for a game I play and we assign guild attacks based on power levels. I'm looking to create a function or script and put it in Column O. I'd like this function to compare Column F to a specific cell in Column M and then return the user associated with Column F that is >= than the specific cell in Column M. Like this enter image description here
I highlighted the first three as an example.
I can obviously do this manually but I takes time, and was looking to automate this process so it becomes more efficient. I've tried Vlookups, MATCH, IF and I've been unsuccessful. Any help would be greatly appreciated. Again, I'm just a beginner with google sheets so please go easy on me. :)
Solution
As you metioned that you would also be happy with an scripted solution I have created this script that I believe solves your issue. It has comments explaining step by step how it works:
function myFunction() {
// Get our sheet
var ss = SpreadsheetApp.getActive().getSheetByName('Automate Test');
// Get the values of the ranges of F and M. Flat will convert the 2D array we get from getValues() into a 1D one which is easier to work with
var valuesF = ss.getRange('F2:F16').getValues().flat();
var valuesD = ss.getRange('D2:D16').getValues().flat();
var valuesM = ss.getRange('M2:M16').getValues().flat();
var valuesN = ss.getRange('N17:N31').getValues().flat();
// We will iterate through all the players in column M to find their opponents
for(i=0;i<valuesM.length;i++){
// We create an empty array to be filled with the list of possible opponents to then choose a random one of the list
var playersHigherEqual = [];
// Iterate through the opponent list
for(j=0;j<valuesF.length;j++){
// If the opponent meets the condition
if(valuesF[j]>= valuesM[i]){
// Add it to the array of possible opponents
playersHigherEqual.push(ss.getRange(j+2, 2).getValue());
}
}
//Finally we will set the opponent by choosing a random one out of the list. Note that i+2 is because the arrays start from 0
ss.getRange(i+2, 15).setValue(playersHigherEqual[Math.floor(Math.random()*playersHigherEqual.length)]);
}
// We will iterate through all the players in column M to find their opponents
for(i=0;i<valuesN.length;i++){
// We create an empty array to be filled with the list of possible opponents to then choose a random one of the list
var playersHigherEqual = [];
// Iterate through the opponent list
for(j=0;j<valuesD.length;j++){
// If the opponent meets the condition
if(valuesD[j]>= valuesN[i]){
// Add it to the array of possible opponents
playersHigherEqual.push(ss.getRange(j+2, 2).getValue());
}
}
//Finally we will set the opponent by choosing a random one out of the list. Note that i+2 is because the arrays start from 0
ss.getRange(i+17, 15).setValue(playersHigherEqual[Math.floor(Math.random()*playersHigherEqual.length)]);
}
}
Please let me know if you also need a sheet formula solution for this question. For more information about Apps Script check out the documentation.
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)
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;
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
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.