CJCoding With Joseph
15free / daySign inCreate account

Three-Node Chain Built With malloc

Build a three-node singly linked list whose nodes live on the HEAP, walk it once, then tear it down. The node type struct Node (with an int value and a struct Node *next) is given.

Read three integers from input. Allocate three nodes with malloc (one per value), link them first to last, and set the last node's next to NULL. Then traverse from the head and print two lines:

- line 1: the values joined by  ->  (space, arrow, space), with no trailing arrow
- line 2: Total:  followed by the sum of the values

For the input:

4 9 2

the output is exactly what is shown in Expected Output.

Free the memory you allocate before the program ends — this question checks for memory leaks.

Expected Output:

4 -> 9 -> 2
Total: 15

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Pointers
📥 Auto-Input:
4 9 2\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (p, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.