Top Coding Algorithms — Linked List

1 min read Original article ↗

Implementing a lined list in Python

Jeremy Zhang

Linked list is a list, but with each element pointing to the next element. So in a linked list, each node contains the current value of the node and a pointer to the next node.

Node

Like the graph shown above, the first node contains a value 2 and a pointer to the next node with value 4, and so on. So the implementation of a node would be:

Linked List

With the component of node implemented, let’s implement a linked list.

Add

So the first and most important function that a linked list need to fulfil is to append a new node to the end of the list.

To append a new node, we need to start from the first node in a linked list, moving to the end of it, and append the new element to the last node.

Reverse