Interview Query

Assessment 1: Singly Linked Lists
Go to question details page

Singly-linked lists are a type of linked list where nodes have only one pointer: A pointer to the next element. Create a class SinglyLinkedList along with its methods:

  • add_head(element) - adds an element to the head of the list. This becomes the new head of the linked list.
  • add_tail(element) - adds an element to the tail of the list. This becomes the new tail of the linked list.
  • remove_head() -> element - removes the head and returns the value of the removed element.
  • remove_tail() -> element - removes the tail and returns the value of the removed element.
  • __contains__(item) -> bool - checks if the item is inside the list. Returns True if it is inside the list and returns False if not. This method is implemented to allow the use of Python’s in operator.
  • __getitem__(index) - returns the element at the specified index. Raises an IndexError if the index is out of bounds. This method is implemented to allow the use of Python’s [] operator.
  • __len__() - returns the length of the SinglyLinkedList.

0%

Completed

You have 20 sections remaining on this learning path.

Advance your learning journey! Go Premium and unlock 40+ hours of specialized content.