1. Learn
  2. /
  3. Courses
  4. /
  5. Data Structures and Algorithms in Python

Connected

Exercise

Inserting a node at the beginning of a linked list

In the previous exercise, you learned how to implement a Node() and LinkedList() class.

In this exercise, you will prepare the code for the insert_at_beginning() method to add a new node at the beginning of a linked list.

Recall the Node() class:

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

Instructions

100 XP
  • Create the new node.
  • Check whether the linked list has a head node.
  • If the linked list has a head node, point the next node of the new node to the head.