dsa.singlylinkedlist module

Module containing singly linked list class.

class dsa.singlylinkedlist.LinkedList(head=None, tail=None, count=0)

Bases: object

A singly linked list implementation.

append(value)

Place a value at the end of the linked list.

Parameters:

value – A value to append.

Returns:

None

Raises:

IndexError – If linked list is empty or index is not found.

delete(value)

Delete the first occurrence of a value in the linked list.

Parameters:

value – The value to be deleted.

Returns:

None

Raises:

ValueError – If the value is not found.

delete_head()

Delete the head node in the linked list. Raise IndexError if linked list is empty.

Returns:

None

Raises:

IndexError – If linked list is empty.

delete_tail()

Delete the last node in the linked list. Raise IndexError if linked list is empty.

Returns:

None

Raises:

IndexError – If linked list is empty.

classmethod from_list(mylist=None)

Create a linked list from a list.

Parameters:

mylist – A list or container to convert from.

Returns:

A linked list containing the items from mylist.

insert_after(after_value, value)

Insert a value after a specified value. Raise exception if value is not found.

Parameters:
  • after_value – The value to insert after.

  • value – The value to append.

Returns:

None

Raises:

ValueError – If value is not found.

is_empty()

Check if the linked list is empty.

Return type:

bool

prepend(value)

Place a value at the beginning of the linked list.

Parameters:

value – A value to append.

Returns:

None

search(value)

Search for a value in the linked list.

Parameters:

value – The value to search for.

Return type:

Node | None

Returns:

Return index of found value. Return None if value is not found.

to_list()

Create a list with contents of the linked list.

Return type:

list

Returns:

List with contents of linked list.

traverse()

Print the contents of the linked list.

class dsa.singlylinkedlist.Node(value)

Bases: object

A singly linked list node implementation.

next

reference to the next node

value

value of the node