E81 CSE 131 Lecture 9: ListNode Methods

24 views2 pages
magentarat813 and 20 others unlocked
E81 CSE 131 Full Course Notes
8
E81 CSE 131 Full Course Notes
Verified Note
8 documents

Document Summary

Compute the length of a list beginning at this listnode public class listnode { Method has access to this as the first listnode in the list. Will provide the length of the smaller list this. next references the smaller list this. next. size() will compute this size. What if there is just a base case. Listnode next; public int size () { if (this. next == null) return 1; else return 1 + this. next. size(); Sum the elements starting at the head of the list. Sum the elements starting at the head of the list public static int sum(listnode head) { int ans = 0; for (listnode p = head; p != null; p = p. next) { ans = ans + p. value; returns ans; Instead, it takes in a parameter (in this case, head) p acts as a reference p = p. next acts the same as i++ for listnodes. Moves the reference p to the next object down the list.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions