Remove duplicates from an unsorted linked list : - java

Input:
N = 4
value[] = {5,2,2,4}
Output: 5 2 4
Explanation:Given linked list elements are
5->2->2->4, in which 2 is repeated only.
So, we will delete the extra repeated
elements 2 from the linked list and the
resultant linked list will contain 5->2->4
Please help me to correct this code..
Let's say an example
1->3->3
Am not able to break th connection
Correct answer should be:
1->3 only

Related

How do i find the common node point of 2 single linked lists?

My approach to this question was to use a hashmap for storing the addresses of first linked list and traversing the second array and checking whether i found the address or not .If it is found i would return the node at that address.
This would take O(m+n),where m and n are lengths of linked list but here we are using O(Linear) space complexity.
I found another approach to this question which has constant space but same time complexity but it didn't clear my doubt.
link to the question
what if my input is
list 11 = 1 -> 2 -> 3 -> 4 -> 6 ->7;
list l2 = 2 -> 9;
In this case my one time traversal of lists after truncating the extra length would become like this
diff=4; // i would advance the list 1 by 4 elements as it has greater length
l1= 6-> 7;
l2= 2 -> 9;
Here i lost my intersection node .
Can someone help me with this?
That method is only valid when the input linked lists are the same from the intersection point like :
1 -> 2 -> 3 -> 4 -> 5
12 -> 11-> 10 -> 3 -> 4 -> 5
Here the two lists are the same from the intersection point 3.
Only then that algorithm works.
Note :
This problem is basically given as to find the intersection point of an inverted Y-shaped list (combination of two lists)

Best way to save last duplicate Entry in a collection?

A collection is given like - [10,11,5,6,10,11,10,11,10,5]
Answer should contains only last element(if element is repeated) Answer - [5,10,6,11]
here element(5) is repeated 2 times so answer should contain 2nd 5
similarly element(10) is repeated 4 times so answer should contain 4th 10
element(11) is repeated 3 times so answer should contain 3rd 11
Note : - here number of time of repetition of element is not matters.
You need to sort the list on the basis of number of repetitions. As #px06 mentioned in the comments you can use set for unique and sorted values. But this will not give you result on the basis of number of repetitions.
Collections.frequency(List, value);
will give the frequency of value in the list.
You need to iterate the list and then put the result into new collection.

Issue in appending and storing the elements in 2d int array in java

I have an issue in adding an array elements and store it into a 2d int array.
An example,
I have a 2d int array(newint2d[][]) which contains 2 elements initially.
0101100000010111101
0011100110111100110
In each iteration will be getting 2 elements.
The looping condition (5times) will produce 10 elements like this randomly at the end of looping statements.
For each iteration i need to add the 2 elements with next 2 and again next 2 like that in 5th iteration i should append the last 2 elements to the 8 elements.
So that i will get 10 elements at final iteration.
And that should store in the 2d int array newint2d[][] in this.
Finally the 2d int array (newint2d[][]) or new 2d int array should contains all the 10 elements stored in it.
For Eg:
**0011100000010111101**
**0101100110111100110** I iteration
*0011100000010111101*
*0101100110111100110* II iteration
0011100000010111101
0101100110111100110
0011100110110111101
0101100000011100110
0101100000010111101
0011100110111100110
How to solve this problem?
If there is something i didn't explain well or you need more code to understand the question let me know.

Find Element that exists in each list in List of Lists

I have a List of Lists in java (grails) and I am trying to find the elements that exist in each list within the list. Does anyone have a quick way to do this? Thank you!
If lists have unique elements you can do it like this (bu unique elements I understand that one element can be placed in few lists, but only once per list. Otherwise if first list contains [1,2,2,3] and other contain [x,2,y] as output you will see [2,2] not [2] )
List tmpList=new ArrayList<>(lists.get(0));
for(int i=1; i<lists.size(); i++)
tmpList.retainAll(new ArrayList<>(lists.get(i)));
System.out.println(tmpList);
take 1 link list, copy it and then check that against all of the other linked lists.
if a list doesn't have an element, than remove that element from your newly made list.
you can implement your newly made list as a map or a hash table to reduce time complexity a bit.
Either way, unless your lists are sorted or something, your algorithm can't be faster than O(n) where n is the sum of all elements in all lists
the algorithm I outlined is O(nm) where m is the count of your smallest list.

about linked list

Hi
I have a question that :
for example I have a linked list that has 4 elements. [1,4,2,7] and their index will be 0,1,2,3.
when I remove the third element which is "2" and it index is 2 ,the index of fourth element will be 2 ,I mean that the index of "7" will be 2? my problem is because of the code bellow.
with doubly linked list we can write such a this code:
(p1--> next) = p3;(p3-->prev)=p1;delete p2;p1 = (p1.prev);
how can i write it for linked list?
thanks
in java you just have the following for a linked list. You don't need to/can't delete a node.
p1.next = p3;
Note: you would only do this if it is homework. In the real world, you should use the builtin, well understood and tested classes.

Categories

Resources