Mental Model
Go through each item in the list one by one from start to end and show its value.
Analogy: Like reading a chain of paper clips one by one to see each clip's color.
head -> 1 -> 2 -> 3 -> null ā
head -> 1 -> 2 -> 3 -> null ā
head -> [curr->1] -> 2 -> 3 -> null
head -> 1 -> [curr->2] -> 3 -> null
head -> 1 -> 2 -> [curr->3] -> null
head -> 1 -> 2 -> 3 -> [curr->null]
1 -> 2 -> 3 -> null
using System; public class Node { public int Value; public Node Next; public Node(int value) { Value = value; Next = null; } } public class LinkedList { public Node Head; public LinkedList() { Head = null; } public void PrintList() { Node curr = Head; // start at head while (curr != null) // traverse until end { Console.Write(curr.Value + " -> "); // print current value curr = curr.Next; // move to next node } Console.WriteLine("null"); // end marker } } public class Program { public static void Main() { LinkedList list = new LinkedList(); list.Head = new Node(1); list.Head.Next = new Node(2); list.Head.Next.Next = new Node(3); list.PrintList(); } }
Node curr = Head; // start at headwhile (curr != null) // traverse until endConsole.Write(curr.Value + " -> "); // print current valuecurr = curr.Next; // move to next nodeConsole.WriteLine("null"); // end markerwhile (curr != null) // traverse until endwhile (curr != null) // traverse until endhead?Node current = head;class Node {
public int data;
public Node next;
public Node(int d) { data = d; next = null; }
}
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
Node current = head;
while (current != null) {
Console.Write(current.data + " -> ");
current = current.next;
}
Console.WriteLine("null");Node current = head;
while (current.next != null) {
Console.Write(current.data + " -> ");
current = current.next;
}
Console.WriteLine("null");Pass the test and this pattern moves unknown