Both of these implementations are bad.
The first is probably worse.
- every node in the linked list, from
self.head to the last node's
head.next is unconditionally written to, which makes every cache-line/cache-page/virtual-memory-page the linked list touches "dirty" and need to be flushed/written.
self.head is passed to
insertHelper(), which recursively calls itself N times, eventually (assuming N > 1) returning the value that it was given
self.head, to be stored back into the
self.head member. This is a LONG critical section. If any threading support was added, locks would need to be held way longer than necessary.
Both use recursion, and Python does not do tail-call-optimization. This means that if your linked-list is very long, you will get a stack overflow. There are manual ways to make your function behave in a tail-call-optimized fashion, even if Python won't do it for you, but they are complex and it is by far simpler to just use a loop.
insertRHelper are helper functions, which should not be exposed a public methods. They should begin with a leading underscore to indicate they are not part of the public interface.
- The members
head, and probably
data should also not be public, so should be named with a leading underscore.
- Classes are named using
BumpyWords; functions, members and variables should all be
snake_case. These means
insertHelper should be named
_insert_helper, and so on.
if(...): is not Pythonic. The parenthesis are unnecessary.
The best solution would be to maintain a
._tail member in the
"""Description of class here"""
self._head = None
self._tail = None
def insert(self, data: Node) -> None:
"""Description of the insert function here"""
self._tail._next = Node(data)
self._tail = self._tail._next
self._head = self._tail = Node(data)
Now, insertions at the tail of the
LinkedList are \$O(1)\$.
As mentioned by Matthieu M. in the comments:
I would also consider renaming here:
insert should be
append, to make it clear where the insert occurs, and offer a path to proposing different insertions such as
insert with an index.