Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create and empty linked list node in python?

According to leetcode this is the definition of a linked list

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

How do you create a single node that when called outputs

[]

instead of

[0]

In the merge two linked lists if we pass two empty nodes then the output is also an empty node, the question is how do you make an empty ListNode that has a value other than 0 or None

like image 443
khalid Avatar asked Jul 20 '26 08:07

khalid


1 Answers

I had the same problem as you but I just figured it out. All you have to do is return None and Leetcode will accept it. I tested it on Leetcode problem 23, Merge k Sorted Lists.

like image 180
Velo Avatar answered Jul 23 '26 04:07

Velo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!