Results 1 to 3 of 3
  1. #1
    helloworld12 is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default Linked List: Removing Largest Element Using Iterator

    how can I remove the largest element from a linkedlist in java? I know I can use get() or remove() functions to retrieve / remove the elements. But I want to make it efficient. I want to use iterator. Do you have any idea how i can do it? Note that I dont want to create my own linkedlist. I dont even want to sort my linkedlist.

    Can I do a linear search for this? If so, how can I keep track of the pointer (or iterator) that points to the largest element. Any help will be appreciated.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    First, you haven't defined what you mean by 'largest element'. I presume you mean largest based upon some sort of Comparator or Comparable. Second, a simple implementation would be to loop over the list and search based upon your criteria - removing the appropriate element when complete.
    But I want to make it efficient. I want to use iterator.
    Why? Efficiency is relative, and searching a List is quite simple and quite effective. Without any context why you want to speed something up and what you're doing advice is all relative, you could use any number of methods - trees, Lists, etc...each of which has their place and may be more efficient in one context, less so in another (relative to other methods)

  3. #3
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    If the list is sorted you can just remove the last element. If not, you would need to iterate through the whole list and check each element.

Similar Threads

  1. linked list iterator
    By TopNFalvors in forum New To Java
    Replies: 3
    Last Post: 03-31-2011, 03:03 AM
  2. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  3. need help with removing repetitions from linked list...
    By OptimusPrime in forum New To Java
    Replies: 8
    Last Post: 03-09-2010, 08:29 PM
  4. Convert Linked List Object element to String
    By CirKuT in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 05:22 AM
  5. Creating an Iterator for Linked List
    By spanky in forum New To Java
    Replies: 3
    Last Post: 10-05-2008, 07:16 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •