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.
Related
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
so i have an array list that contains strings such as:
ArrayList<String> list = new ArrayList<>();
list.add("bookshelf");
list.add("bookstore");
list.add("library");
list.add("pencil");
Now i wanna search and remove all the strings in the arraylist that contain the word "book" in them. As far as i understand list.remove("book"); will only search for the particular string "book" and not the strings that contain the word "book". How can i solve this?
You can use removeIf like this:
list.removeIf(s -> s.contains("book"));
Note: this answers applies to Java version 7 and below (of course that it will work for higher versions as well but YCF_L's answer is simpler to implement in versions 8 and above).
The requirement is to iterate the list, check every element, and if it answers a certain condition: remove it.
Since this is the case we fall into a risky scenario where we modify the list while iterating it which is problematic because when we remove an element in the list its size changes.
In order to work around this problem we can iterate the list by index from the last element and back until the first one, this way, removing an element at index n will not effect accessing any element at index < n.
I'll leave the implementation details to you in order not to "spoon feed" and destroy your exercise :)
Do I have to search for the node as a stack or queue, and when I find it set the data that the node returns equal to local variables?
Or is there a way of calling a known position in the list, like how you can with an array.
Visually what I'm asking here is:
So how Arrays work like this:
ArrayofStrings[Postion] returns String located at Position
Is there a way to do the same with a Linked List like this?
SinglyLinkedList(Node) returns Multiple(string) Data(double)
I'm guessing I have to search the list for the node, but I wanted to make sure there wasn't an easier way to do this.
LinkedLists pros and cons
pros
traversing
adding or removing anywhere because you just have to
change two pointers(reference in Java)
cons
get by index because it will traverse through elements until it finds the specific one (doubly linked list may be a little faster because if you have total
count you can start from beginning or end depending on the index ie
if there are 100 elements and you want to goto 75 you can start from
the other end which will only require to traverse 25 elements)
Arrays pros and cons.
pros
traversing (even subset),
adding data at the end
getting element from a specific location.
cons
If you add or remove element from the middle it will shift all the elements after that location.
All lists in java provide an implementation of get, which returns the n'th element in list.
Two Singly linked Lists, size m , r and want to insert the first linked list nodes after the head of the second linked list, and the time complexity has to be O(1) of the method.
This really an intereseting difficult problem for me. Eatch time I think of a solution, the Time complexity is O(m+r)
I need some hints to solve this. I consumed useless effort on this problem.
EDIT:
Let me share what I have so far:
Create a new Linked List
Add the HEAD of the 2nd list
Still O(1)
Add all the nodes of 1st list
Becomes (n)
Add the rest of the nodes from the 1st list
Becomes another (n-1)
UPDATE:
What do you think about this? I got inspired directly after I asked here :)
If you have two singly-linked lists and don't have the tail of the first already, this is only possible in O(n). If you have the tail you simply make it point to the head of the second list...
Edit: 2nd list head points to first list's head. Hold a reference to 2nd list's 2nd node. Iterate down first list - again this is O(n) if you don't have a reference to the tail to start - and have the tail of that point to the original 2nd element of the 2nd list.
Assuming you have these structures:
List
Head node
Tail node
Node
Value
Next node
Reminder to self: the goal is: "insert the first linked list nodes after the head of the second linked list".
Then all you've got to do is:
// Hook up the end of list1 to the original second element of list2
list1.tail.next = list2.head.next;
// Set the second element of list2 to be the first element of list1
list2.head.next = list1.head;
List2 still ends where it did before (its tail node is the same).
You've now got list1 with a "floating" head, which is generally bad news... but if you iterate over list1 you'll get all the elements from both original 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.