Java program to traverse a single linked list?
-
-
Answer:
There are several ways to traverse a LinkedList object in Java... LinkedList<Integer> nums = new LinkedList<Integer>(); // Assume nums is filled with some ints. We don't care which. // Very inefficient method. This method has a O(n2) efficiency, since each time you call the // LinkedList.get(n) method, you're iterating through the first n items. for(int i = 0; i < nums.size(); ++i) { System.out.println(nums.get(i)); } // More efficient method. This method has a O(n) efficiency, since you only iterate once per // item. for(Iterator<Integer> it = nums.iterator(); it.hasNext();) { System.out.println(it.next()); } // Equally efficient as above. Uses some Java syntactic sugar to mask the above // implementation in a nicer presentation using the for-each loop. for(Integer n:nums) { System.out.println(n); }
community wiki at wiki.answers.com Visit the source
Related Q & A:
- how to draw circular doubly linked list?Best solution by TeX - LaTeX
- How to forward e-mail to a group from a single e-mail address?Best solution by Webmasters
- Is it possible to be a travel nurse when your a single mom of 1 child?Best solution by Yahoo! Answers
- Can a person have more than two alleles for a single gene?Best solution by answers.yahoo.com
- How to create this Java program?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.