35 lines
941 B
Markdown
35 lines
941 B
Markdown
```java
|
|
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.
|
|
|
|
```java
|
|
temp.info += 50;
|
|
```
|
|
|
|
b) Write code to remove from the list the item after temp.
|
|
```java
|
|
temp.next = temp.next.next;
|
|
```
|
|
|
|
c) Write the code to remove from the list temp.
|
|
```java
|
|
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.
|
|
|
|
```java
|
|
IntNode current = head;
|
|
int sum = 0;
|
|
while ( current != null ) {
|
|
sum += current.info;
|
|
current = current.next;
|
|
}
|
|
``` |