How to traverse linked list in c++?

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

Was this solution helpful to you?

Just Added Q & A:

Find solution

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.