| Author |
finding the maximal number in the linked list
|
alex lotel
Ranch Hand
Joined: Feb 01, 2008
Posts: 191
|
|
i have a class: i want to build a method which return a maximal number in the whole list is it ok to implement it like this??
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
More of a beginner's question. Have you been told to use a for loop? I think you are better using a while loop. (Or even a do loop.) Set up a Node local variable, which of course you initialise to the first node in your series, then at the end of the loop you reassign it to the next node. The test is while(myNode != null) or similar. BTW: Start by initialising your max local variable to Integer.MIN_VALUE. Look through the Integer class API to see whether I have spelt it correctly. BTW2: Setting up list.next = list.next.next is dangerous; you may actually be reassigning a member of the list rather than iterating along it.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
Another error I hadn't noticed earlier. Your return type is incorrect. BTW: You oughtn't to use all those capital letters in the method name. You don't actually need the local variable "temp."
|
 |
 |
|
|
subject: finding the maximal number in the linked list
|
|
|