Skip to content

Singly_Linked_List: extra O(n) time complexity while getting length of the list #5315

Closed
@jssparas

Description

@jssparas

While getting the length of the linked list, code will take O(n) of unnecessary time as it executes -
len(tuple(iter(self)))
This also takes up O(n) of extra space as it is creating a tuple first.

This can be easily avoided by maintaining an instance variable len, increment/decrement it while inserting/deleting elements.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions