• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to delete an element in the middle of the linked list?  RSS feed

 
Suna Dev
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


dear friends that is my own code. So how to delete an element in the middle of the linked list? help me
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suna Dev wrote:So how to delete an element in the middle of the linked list? help me

Work out the index of the middle element and then use the LinkedList.remove(int index) method.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the API specs for LinkedList.removeFirstOccurrence

Edit, actually just LinkedList.remove as suggested above.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As has already been said if you know the index of the element you want to remove then you can just do it directly. I've demonstrated this in the code snip below.
You can also iterate (loop) through the list and then delete elements from within the loop. This is handy if you want to delete elements that contain the String "4". Also demonstrated here.

I couldn't help but tidy up your code a little as I am a firm believer that class and variable naming is very important. I also updated your LinkedList usage to be Java 5 compliant with the type parameterisation (LinkedList<String>) and also Java 7 compliant with the diamond notation in the constructor (new LinkedList<>()).

Hope that helps you explore the things you can do with a LinkedList. Note that the use of Iterator is not specific to LinkedLists and that it is part of the Collection interface.
 
Suna Dev
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok but how to find the middle?
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends on how you define "the middle"? For me that means any element that is not the first or the last element.

Can you elaborate to define exactly what you mean?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suna Dev wrote:Ok but how to find the middle?

Find a size of the list and divide it by 2. That works very well if size of the list is odd. What if it is even? It depends on you.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:

Alternatively, you could do
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:Alternatively, you could do

Oh that's nice. I like it.
 
Suna Dev
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for every one!!!
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:
Stuart A. Burkett wrote:Alternatively, you could do

Oh that's nice. I like it.

I was thinking about this last night and whilst it might look nice, it's probably pretty inefficient.
The iterator solution above iterates the list only once, whereas this solution probably starts a new iteration each time remove is called. Although each iteration will be cut short (when a match is found), the last one (once there are no more matching instances in the list) will be a full iteration.
So the last iteration is the equivalent of the above solution and any other iteration (the ones that find a match) will be extra processing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!