Learn Before
Concept

Linked List

A linked list is composed of a chain of objects known as nodes. Each node contains a value and a pointer to the next node in the chain. The first node in a list is known as the head. The last element in the list points to null (indicating the end of the list).

Example implementation of a Node class:

class Node: def __init__(self, value, next = None): self.value = value self.next = next

The following example is a Linked List class that creates a single head node:

class LinkedList: def __init__(self): self.head = None

The following snippet creates a linked list with 3 elements:

my_linked_list = LinkedList() # create a LinkedList object my_linked_list.head = Node("Jon") # sets the head's value to "Jon" element2 = Node("Sarah") # create a Node with value "Sarah" element3 = Node("Matt") # create a Node with value "Matt" my_linked_list.head.next = element2 # sets the head's next element to element2 element2.next = element3 # sets element2's next element to element3

0

1

Updated 2021-04-13

Contributors are:

Who are from:

Tags

Python Programming Language

Data Science

Learn After