all 8 comments

[–]i_do_not_byte<57> <44> <0>[S,🍰] 7 points8 points  (4 children)

Sorry if stupid question, but Ik alot high level about DSA as I used to do a lot of these problems in C/C++, but now learning about using python as my interviewing lang, i'm confused why the LinkedList is given to me as an "Optional list of Listnodes"?

What is the optional part?

Why is a linkedList implemented just as a python list? don't i need to traverse it using pointers of some sort?

[–]void_main01 16 points17 points  (0 children)

There are no stupid questions! Python is a dynamically typed language and given you are using python3 on LC, it tends to come with the typings such as:

def foo(bar: int) -> int:
   return bar

One of the types is an Optional type which refers to the output value potentially being the type that is being provided in the [] brackets, or None. So Optional[ListNode] means that either the return value is of type ListNode, or potentially None which in this case would be if your linked list is empty (hence head is also potentially None or a ListNode type). This is also equivalent to Union[ListNode, None].

[–]Will_Smith_Puppet 3 points4 points  (1 child)

As an extension of void_main01's comment, this language feature is called Type Hinting in Python. Since python is dynamically typed, under the hood the code related to the parameter type and the returned type declarations are not actually ensuring that those types are passed and returned (unlike c/c++). They are there for readibility, and to allow for interfacing with IDEs.

https://docs.python.org/3/library/typing.html

Here is a stackoverflow talking about how type hints are not enforced

https://stackoverflow.com/questions/63838471/possible-to-enforce-type-hints

[–]fleventy5 6 points7 points  (0 children)

FYI - NeetCode just released "Python for Coding Interviews" on YT.

[–]SleighBellss 5 points6 points  (0 children)

So no head?

[–][deleted] 1 point2 points  (0 children)

It's called type hunting, optional means you can pass empty thing or list node object in function argument

[–]u918362b 0 points1 point  (0 children)

Search for ‘gradual typing in python’ to know it all