Results 1 to 9 of 9
  1. #1
    OptimusPrime is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default need help with removing repetitions from linked list...

    so... i have a linked list, i need to delete all repetitions from the list.
    i started working on it and i have 3 loops already... it turned out to be a real mess.
    i wanted to treat it like arrays but it so much different when moving with pointers on a linked list.
    the general idea is to take the first element and every time i find the same data on the list i delete it. by deleting i mean skipping this element.
    can anyone help me with this one?
    thanks,
    OP

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Optimus why don't you use Set - A collection that contains no duplicate elements. If you can create that collection at start like that - then there are no problems later.

    If you are iterating through collection and removing elements at the same time - take care - use an Iterator.

    Or maybe convert list to set like:

    Java Code:
    List<String> myList = new ArrayList<String>();
    		    myList.add("A");
    		    myList.add("B");
    		    myList.add("A"); //duplicate
    		    myList.add("D");
    
    		    Set<String> mySet = new HashSet<String>(myList);
    
    		    for (Object theFruit : mySet)
    		      System.out.println(theFruit); //no duplicate
    		  }
    Anyway i stongly recommend reading this small article about
    choosing right collection :

    Java Practices -> Choosing the right Collection

    hope you will solve it easily! cheers!

  3. #3
    OptimusPrime is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default

    hello,
    first, thanks for your reply.
    2nd, i never wrote it, but its an assignment i have to do. its kinda complicated and i really want to figure out how to do it.
    so switching to a set is not an option right now...

    any other ideas?
    thanks...

  4. #4
    OptimusPrime is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default

    help anyone?

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You did describe the algorithm very well, now, are you supposed to use the already implemented LinkedList, or make your own representation of the structure? You description contains all the steps you need to do, so it shouldn't be too difficult to translate it into code.

  6. #6
    OptimusPrime is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default

    i already have the linked list. i need to work on it.
    i know i have all the steps im just messing up the pointers...
    really, i cant make it work.

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    What have you tried? Which code failed?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    OptimusPrime is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default

    im stuck inside the 2nd loop.
    i dont know how to connect between the pointers after i find 2 that identical...

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by OptimusPrime View Post
    i started working on it and i have 3 loops already... it turned out to be a real mess.
    If you don't want to use FON's suggestion then two loops are enough: 1) to walk over all elements of the list and 2) another one to remove the duplicates pointed to by 1)

    kind regards,

    Jos

Similar Threads

  1. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM
  2. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 03:15 AM
  3. Linked List
    By rnavarro9 in forum New To Java
    Replies: 0
    Last Post: 11-29-2007, 03:42 AM
  4. Help with linked list
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:29 AM

Posting Permissions

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