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.
Answer from Velo on Stack OverflowI 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.
If you want to create Empty Linked List and assuming head is given.
curr = head
curr.next = None
curr = curr.next
I am solving a practice problem in which I have to combine two linked lists to create a new sorted linked list. In the problem, we have the following two linked lists: l1 = [1,2,4] and l2 = [1,3,4] and I create a sorted linkedlist l3.
I write l3 = ListNode() to defined an empty linked listI am able to successfully create a sorted list except I get [0, 1, 2, 2, 3, 4] instead of [1,2,2,3,4] as the resulting sorted list**.** I tried another approach by defining l3 = ListNode(None) to obtain an empty linked list before running the rest of my code. However, the output now is [None, 1,2,2,3,4].
How can I define an empty linked list, so that I can get the output [1,2,2,3,4] ?
how do create a linked list in python - Stack Overflow
How to create linked list?
python - Create a linked list without initializing an empty node - Stack Overflow
[Project Help] Reading from a text-file, and putting it into a linked list. Need someone to point me in the right direction.
You need two "pointers" that memorize the head and the tail of your list. The head is initialized once. You will eventually use it to access the whole list. The tail changes every time you add another node:
data = [5, 1, 7, 96]
tail = head = ListNode(data[0])
for x in data[1:]:
tail.next = ListNode(x) # Create and add another node
tail = tail.next # Move the tail pointer
dummy = ListNode()
prev = dummy
for val in arr:
node = ListNode(val)
prev.next = node
prev = node
dummy.next - is the link to the first element of created linked list
You have to assign the head in the append function if there's no head already. The head was always an Empty Node
class Node:
def __init__(self, item=None):
self.item = item
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def showElements(self):
curr = self.head
while curr is not None:
print(curr.item)
curr = curr.next
def append(self, item):
if self.head is None:
self.head = Node(item)
return
new_node = Node(item)
curr = self.head
while curr.next is not None:
curr = curr.next
curr.next = new_node
Your append method sets head.next to new_node, but it never sets head.item, which is why you get output=None.
So with this implementation, the head doesn't store an actual element, it's just used as a starting point for the list. That's not necessarily a problem, it's one implementation choice.
You could also change the implementation to store an item in head, why not.
Whatever the choice, I would suggest implementing first() and next() methods to hide the implementation details.