# Finding length of linked list

Page 1 of 1

## 4 Replies - 24369 Views - Last Post: 06 April 2007 - 10:29 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=26025&amp;s=2544569c9c3ce8cfb14fdbfaa87d267e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 system0utprint

• New D.I.C Head

Reputation: 0
• Posts: 14
• Joined: 29-January 07

# Finding length of linked list

Posted 05 April 2007 - 06:05 PM

I'm supposed to find the length of a linked list, made of Node objects. Each node stores an integer and a pointer to the next node (or null). I already tested this method on a sample linked list with integers...

```public int length(){
int counter = 0;
Node temp = head;
while(temp!=null){
temp = temp.getNext();
counter++;
}
```

So if I say System.out.println((whatever).length());, it's supposed to print out a number, but my program always prints out a 0 no matter what. Apparently something's wrong but I'm not sure what...any help is very much appreciated! Thank you!

Is This A Good Question/Topic? 0

## Replies To: Finding length of linked list

### #2 William_Wilson

• lost in compilation

Reputation: 207
• Posts: 4,812
• Joined: 23-December 05

## Re: Finding length of linked list

Posted 05 April 2007 - 06:08 PM

you'll need to post the whole method, where does the value head come from?
Is head's next value == null maybe?

### #3 system0utprint

• New D.I.C Head

Reputation: 0
• Posts: 14
• Joined: 29-January 07

## Re: Finding length of linked list

Posted 05 April 2007 - 06:48 PM

Hi!
Here's the class with constructors, etc... I think that head points to the first node in the list. So in the method I created a temp that started at the first node in the list and moved along the list, keeping an incrementing counter. But it doesn't work...

Thanks for any help! I'll post more code if necessary.

```//here's part of the class

/** Default constructor: sets head to null **/
}

/** num of nodes in list **/
public int length(){
Node temp = head;
int counter = 0;
while(temp!=null){
temp = temp.getNext();
counter++;
}
return counter;
}
}

```

This post has been edited by system0utprint: 05 April 2007 - 06:51 PM

### #4 William_Wilson

• lost in compilation

Reputation: 207
• Posts: 4,812
• Joined: 23-December 05

## Re: Finding length of linked list

Posted 05 April 2007 - 06:52 PM

assuming that the method getNext() works properly, the code should work jsut fine. My guess is that the list created (there is a list created?) is empy or is only one element.

### #5 system0utprint

• New D.I.C Head

Reputation: 0
• Posts: 14
• Joined: 29-January 07

## Re: Finding length of linked list

Posted 06 April 2007 - 10:29 AM

Hi, thanks for the help! I'll go check the list to see what's wrong.

This post has been edited by system0utprint: 06 April 2007 - 10:30 AM