aspose file tools
The moose likes Java in General and the fly likes finding the maximal number in the linked list Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "finding the maximal number in the linked list" Watch "finding the maximal number in the linked list" New topic
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
    
    4
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
    
    4
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."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finding the maximal number in the linked list
 
Similar Threads
another finding out of what function "what" does?
Saving to a txt file
My LinkedList Iterator - Can't print first element - No use of Java API
Java Linked List
Can nodes contain objects?