Files
G4G0-1/Semester 2/Programming 2/Week 10 Revision/Q4.md

941 B

public class IntNode
{
public int info;
public IntNode next;
}

a) Write code to add 50 to the appropriate field of the node pointed to by temp.

temp.info += 50;

b) Write code to remove from the list the item after temp.

temp.next = temp.next.next;

c) Write the code to remove from the list temp.

temp.info = temp.next.info; // Assign the next node's value to temp - this will overwrite temp's value.
temp.next = temp.next.next; // Set the pointer for temp's next node to the node after the value taken for temp - this effectively clones temp.next and replaces temp.

d) Write code to go through the list and to sum all the values within the list. You may assume the existence of head, a reference to the start of the list.

IntNode current = head;
int sum = 0;
while ( current != null ) {
	sum += current.info;
	current = current.next;
}