Helpers

Updated: 2023-02-11
1 min read

On This Page

Create Linked List

Definition for singly-linked list:

class Node:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
values = [2, 4, 3]

def create_linked_node(values):
    head = Node(values[0]) # start node, head of the linkedlist
    current = head   # current node in the linked list where we change/add next node
    for i in values[1:]:
        node = Node(i)
        current.next = node
        current = current.next  # now current node is last created
    return head

linked_list = create_linked_node(values)

Object structure:

create linkedlist in python