| 1 | Create new node with data=10 | Empty | newNode: data=10, prev=null, next=null | ā
(empty list) |
| 2 | Check if list is empty | Empty | head == null ā True | ā
(empty list) |
| 3 | Set head = newNode | 1 node | head ā newNode | āāāāāāāāāā
ā data:10ā
ā prev:ā
ā
ā next:ā
ā
āāāāāāāāāā
head |
| 4 | Create new node with data=20 | 1 node | newNode: data=20, prev=null, next=null | āāāāāāāāāā
ā data:10ā
ā prev:ā
ā
ā next:ā
ā
āāāāāāāāāā
head |
| 5 | Check if list is empty | 1 node | head == null ā False | āāāāāāāāāā
ā data:10ā
ā prev:ā
ā
ā next:ā
ā
āāāāāāāāāā
head |
| 6 | Start at head to traverse | 1 node | current ā head (data=10) | āāāāāāāāāā
ā data:10ā
ā prev:ā
ā
ā next:ā
ā
āāāāāāāāāā
head/current |
| 7 | Traverse to last node (current.next == null) | 1 node | current.next == null ā True (stop) | āāāāāāāāāā
ā data:10ā
ā prev:ā
ā
ā next:ā
ā
āāāāāāāāāā
head/current |
| 8 | Link newNode at end: current.next = newNode | 2 nodes | current.next ā newNode | āāāāāāāāāā āāāāāāāāāā
ā data:10ā ā data:20ā
ā prev:ā
ā ā prev:ā
ā
ā next:āāā¼āāā ā next:ā
ā
āāāāāāāāāā āāāāāāāāāā
head newNode |
| 9 | Set newNode.prev = current | 2 nodes | newNode.prev ā current | āāāāāāāāāā āāāāāāāāāā
ā data:10ā ā data:20ā
ā prev:ā
āāāā ā prev:āāā¼
ā next:āāā¼āāā ā next:ā
ā
āāāāāāāāāā āāāāāāāāāā
head newNode |
| 10 | Create new node with data=30 | 2 nodes | newNode: data=30, prev=null, next=null | āāāāāāāāāā āāāāāāāāāā
ā data:10ā ā data:20ā
ā prev:ā
āāāā ā prev:āāā¼
ā next:āāā¼āāā ā next:ā
ā
āāāāāāāāāā āāāāāāāāāā
head current |
| 11 | Check if list is empty | 2 nodes | head == null ā False | āāāāāāāāāā āāāāāāāāāā
ā data:10ā ā data:20ā
ā prev:ā
āāāā ā prev:āāā¼
ā next:āāā¼āāā ā next:ā
ā
āāāāāāāāāā āāāāāāāāāā
head current |
| 12 | Start at head to traverse | 2 nodes | current ā head (data=10) | āāāāāāāāāā āāāāāāāāāā
ā data:10ā ā data:20ā
ā prev:ā
āāāā ā prev:āāā¼
ā next:āāā¼āāā ā next:ā
ā
āāāāāāāāāā āāāāāāāāāā
head/current |
| 13 | Traverse: current.next != null? Yes (move current) | 2 nodes | current ā current.next (data=20) | āāāāāāāāāā āāāāāāāāāā
ā data:10ā ā data:20ā
ā prev:ā
āāāā ā prev:āāā¼
ā next:āāā¼āāā ā next:ā
ā
āāāāāāāāāā āāāāāāāāāā
head current |
| 14 | Traverse: current.next != null? No (stop) | 2 nodes | current.next == null ā True | āāāāāāāāāā āāāāāāāāāā
ā data:10ā ā data:20ā
ā prev:ā
āāāā ā prev:āāā¼
ā next:āāā¼āāā ā next:ā
ā
āāāāāāāāāā āāāāāāāāāā
head current |
| 15 | Link newNode at end: current.next = newNode | 3 nodes | current.next ā newNode | āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
ā data:10ā ā data:20ā ā data:30ā
ā prev:ā
āāāā ā prev:āāā¼āāā ā prev:ā
ā
ā next:āāā¼āāā ā next:āāā¼āāā ā next:ā
ā
āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
head current newNode |
| 16 | Set newNode.prev = current | 3 nodes | newNode.prev ā current | āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
ā data:10ā ā data:20ā ā data:30ā
ā prev:ā
āāāā ā prev:āāā¼āāā ā prev:āāā¼
ā next:āāā¼āāā ā next:āāā¼āāā ā next:ā
ā
āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
head current newNode |
| 17 | End of insertion | 3 nodes | List updated with new node at end | āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
ā data:10ā ā data:20ā ā data:30ā
ā prev:ā
āāāā ā prev:āāā¼āāā ā prev:āāā¼
ā next:āāā¼āāā ā next:āāā¼āāā ā next:ā
ā
āāāāāāāāāā āāāāāāāāāā āāāāāāāāāā
head node2 new tail |