This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: would return 9. /* This solution uses a boolean variable that alternates on every pass through the while loop in order to add up every other value. */ public int sumAlternate() { Node current = head; int sum = 0; boolean everyOther = true; while(current != null) { if(everyOther) { sum += current.value; everyOther = false; } else everyOther = true; current = current.next; } return sum; } 3.Write a method sumEvens that computes and returns the sum of all even numbers in the list. .For example, if the original list is: 1 2 3 4 5 this method would return 6. public int sumEvens() { Node current = head; int sum = 0; while(current != null) { if(current.value % 2 == 0) sum += current.value; current = current.next; } return sum; }...
View
Full
Document
This note was uploaded on 06/11/2009 for the course CS 205214 taught by Professor Balasooriya/kouvetakis during the Spring '09 term at ASU.
 Spring '09
 Balasooriya/Kouvetakis

Click to edit the document details