| 1 | Create new node with data=10 | ∅ | newNode: data=10, next=null | No nodes yet |
| 2 | Check if list is empty (head=null) | ∅ | Condition true | List empty |
| 3 | Set newNode.next = newNode (self-loop) | newNode(10) | newNode.next → newNode | ┌────────┐
│ data:10│
│ next:─┐│
└───────┘│
└─→ (points to self) |
| 4 | Return newNode as head | newNode(10) | head → newNode | ┌────────┐
│ data:10│
│ next:─┐│
└───────┘│
└─→ (points to self)
head |
| 5 | Insert 20 at end: Create new node | newNode(10) | newNode2: data=20, next=null | Existing: 10 → points to self
New: 20 → null |
| 6 | Start traversal at head (10) | newNode(10) | current → head (10) | ┌────────┐
│ data:10│
│ next:─┐│
└───────┘│
└─→ (points to self)
head/current |
| 7 | Check current.next == head? (10.next == 10) | newNode(10) | Condition true, stop traversal | At last node (10) |
| 8 | Set current.next = newNode2 (20) | 10 → 20 | 10.next → 20 | ┌────────┐ ┌────────┐
│ data:10│──→ │ data:20│
│ next:─┐│ │ next:null│
└───────┘│ └────────┘
└─────────────→ |
| 9 | Set newNode2.next = head (10) | 10 → 20 → 10 (circular) | 20.next → 10 | ┌────────┐ ┌────────┐
│ data:10│──→ │ data:20│──→
│ next:─┐│ │ next:─┐│
└───────┘│ └───────┘│
└─────────────┘
head back to head |
| 10 | Return head (10) | 10 → 20 → 10 (circular) | head unchanged | ┌────────┐ ┌────────┐
│ data:10│──→ │ data:20│──→
│ next:─┐│ │ next:─┐│
└───────┘│ └───────┘│
└─────────────┘
head back to head |
| 11 | Insert 30 at end: Create new node | 10 → 20 → 10 (circular) | newNode3: data=30, next=null | Existing: 10 → 20 → 10
New: 30 → null |
| 12 | Start traversal at head (10) | 10 → 20 → 10 | current → 10 | ┌────────┐ ┌────────┐
│ data:10│──→ │ data:20│──→
│ next:─┐│ │ next:─┐│
└───────┘│ └───────┘│
└─────────────┘
head/current |
| 13 | Check current.next == head? (10.next == 20) | 10 → 20 → 10 | Condition false, move current to 20 | current moves to 20 |
| 14 | Check current.next == head? (20.next == 10) | 10 → 20 → 10 | Condition true, stop traversal | At last node (20) |
| 15 | Set current.next = newNode3 (30) | 10 → 20 → 30 | 20.next → 30 | ┌────────┐ ┌────────┐ ┌────────┐
│ data:10│──→ │ data:20│──→ │ data:30│
│ next:─┐│ │ next:─┐│ │ next:null│
└───────┘│ └───────┘│ └────────┘
└─────────────┘ |
| 16 | Set newNode3.next = head (10) | 10 → 20 → 30 → 10 (circular) | 30.next → 10 | ┌────────┐ ┌────────┐ ┌────────┐
│ data:10│──→ │ data:20│──→ │ data:30│──→
│ next:─┐│ │ next:─┐│ │ next:─┐│
└───────┘│ └───────┘│ └───────┘│
└─────────────┘
head back to head |
| 17 | Return head (10) | 10 → 20 → 30 → 10 (circular) | head unchanged | ┌────────┐ ┌────────┐ ┌────────┐
│ data:10│──→ │ data:20│──→ │ data:30│──→
│ next:─┐│ │ next:─┐│ │ next:─┐│
└───────┘│ └───────┘│ └───────┘│
└─────────────┘
head back to head |
| 18 | Stop: No more insertions | 10 → 20 → 30 → 10 (circular) | End of operations | Final circular list with 3 nodes |