Interview Query

Singly Linked List

Start Timer

0:00:00

Upvote
1
Downvote
Save question
Mark as completed
View comments (2)
Next question

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.
.
.
.
.
.


Comments

Loading comments.