

InterviewSolution
Saved Bookmarks
1. |
A linked list is formed from the objects of the class,class ListNodes { int item; ListNodes next; } Write a method OR an algorithm to compute and return the sum of all integers items stored in the linked list. The method declaration is specified below: int listsum(ListNodes start); |
Answer» Algorithm Let P be a pointer of type listNodes. 1. P = start, sum = 0 2. Repeat steps 3, 4 while (P! = NULL) 3. sum = sum + P → item 4. P = P → next (end of step 2 loop) 5. Return sum (Print sum) |
|