Results 1 to 3 of 3
  1. #1
    colerelm is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default How would I count number of nodes in linked list?

    I have a linked list of type generic and I want to count how many nodes there are in the linked list....How, conceptually, should I think of approaching this? I can't seem to wrap my head around it.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: How would I count number of nodes in linked list?

    Maintain an instance variable. Each time a node is added increment it by one. Each time a node is deleted decrement it by one.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How would I count number of nodes in linked list?

    How, conceptually, should I think of approaching this?
    Have you ever been in one of those "mystery tour" type competitions where you get instructions via a clue which lead you to the location of the next clue. Great fun especially if the nodes you are led to are pubs about the place.

    Now it's a relatively simple matter to count the number of places you visit as you follow the clues. If you stick to orange juice.

Similar Threads

  1. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  2. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  3. Replies: 0
    Last Post: 11-29-2010, 01:27 AM
  4. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  5. linked list nodes all refernce same item.
    By yllawwally in forum New To Java
    Replies: 0
    Last Post: 12-18-2007, 08:45 PM

Posting Permissions

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