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 Overflow
🌐
Reddit
reddit.com › r/learnpython › how to create an empty linked list?
r/learnpython on Reddit: How to create an empty linked list?
July 5, 2021 -

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] ?

Discussions

how do create a linked list in python - Stack Overflow
I am trying to solve a linked list coding challenge in python. And I have given only following class to create a linked list # Definition for singly-linked list. class ListNode(object): def More on stackoverflow.com
🌐 stackoverflow.com
How to create linked list?
Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions. More on geeksforgeeks.org
🌐 geeksforgeeks.org
1
March 21, 2024
python - Create a linked list without initializing an empty node - Stack Overflow
I create a linked list using python class. But I can't manipulate the output format: class Node: def __init__(self, item=None): self.item = item self.next = None class LinkedL... More on stackoverflow.com
🌐 stackoverflow.com
February 7, 2021
[Project Help] Reading from a text-file, and putting it into a linked list. Need someone to point me in the right direction.
I will now use meaningful names for classes if you don't mind, so it makes more sense. First of all,we need to start with creating a single link, or a Node (or your "DEF" object). public class Node { Date data; // the payload, which will be a single date entry from the text file Node next; //pointer to the next link in chain Node prev; //pointer to the previous link in chain public Node(Date date) { //a constructor that takes a Date value as parameter and stores it in a link, pointing to nothing this.data = date; this.next = null; this.prev = null; } } A doubly-linked list will maintain a chain of Nodes and keep track of its size: class MyLinkedList { int size; Node head; //we need to know where to start from, so we keep a reference to the first link Node tail; //pointer to the end of the list //..other methods, including addNode(), removeNode(), //get(index), isEmpty(), etc. } The assignment requires to keep two sentinel nodes as well - i.e. empty head and tail nodes. Sentinels contain no payload (no data) and are pointing to nulls, just so we can safely reach the end of the list and avoid a NullPointerException. I don't want to do your entire homework for you, so I recommend you read up on doubly linked list implementation with sentinel nodes in your textbook/lecture notes. If you don't have any for some reason, here's a quick reference you can use or just google "java doubly linked list implementation example" anywhere. Next, you're gonna tie it all together - read text from a file line by line, extract each date, wrap it in a single Node and add the Node to the linked list. To read data from a file, google "java Scanner" examples, it's fairly easy. Then depending on the file's format, you will need to figure out how to extract a single date from a line of input. But the lines you read are of type String (or simple text). In my example I'm assuming thre's no more data in a single line but a single date in plain text format. To convert a String to a valid Date (not sure if required, but you mentioned error checks so I'm assuming it is) you might want to use the DateFormat.parse(String) method and handle the ParseException as your prof's heart desires. Here's how it should go in a nutshell: public class ABC { String myFile; public ABC(String myFile) { this.myFile = myFile; } The reason I don't put the input code inside the constructor is that it's considered a bad practice. Constructors should return very fast and avoid using methods that can throw IO exceptions - in this particular case at least (until you learn about finalizers and such)/ Instead, I took the input out to the separate method doInput() which will return a ready-made LinkedList made of your text file data: public MyLinkedLIst doInput() { //use Scanner to read from myFile, convert each line to a Date //and wrap it in a Node: Scanner myScanner = null; try { myScanner = //instantiate a Scanner object here with //the passed file name (myFile) MyLinkedList myList = new MyLinkedList(); while (myScanner.hasNextLine()) { String line = //read next line from the scanner try { Date myDate = DateFormat.parse(line.trim()); Node node = //wrap myDate in a Node instance myList.add(node); catch (ParseException e) { System.out.println("Could not parse line " + line); } catch (IOException e) { System.out.println("ERROR: Could not open file " + myFile); } } Then in a main() method somewhere, be it your ABC class, or somewhere else, you will instantiate an object of type ABC with your textfile name as a single argument, and call doInput() on it to receive a linked list of dates to do with as you please. All you have to do now is implement the MyLinkedList class as mentioned above. (and then display it with JFrame but that part I hope you can handle yourself'). More on reddit.com
🌐 r/javahelp
7
2
November 16, 2014
🌐
Medium
medium.com › @gichehakevin › implementing-a-singly-linked-list-with-python-41d38b6d6131
Implementing A Singly Linked List In Python | by Kevin Gicheha | Medium
December 20, 2023 - def insert(self, index, data): ... self.length += 1 return True my_llist.insert(2, 300) ''' 1. if the given index is 0, utilize the PREPEND function 2. if the given index is equal to the number of nodes in the list, utilize ...
🌐
Real Python
realpython.com › linked-lists-python
Linked Lists in Python: An Introduction – Real Python
June 24, 2026 - First, you need to create a linked list. You can use the following piece of code to do that with deque: ... The code above will create an empty linked list.
🌐
Graphy
nerchukoacademy.graphy.com › blog › creating-a-single-linked-list
Creating a Single Linked List - Nerchuko
class Node: def __init__(self, data, next=None): self.data = data self.next = next class LinkedList: def __init__(self): self.head = None def print_list(self): if self.head is None: print("The linked list is empty") return current = self.head while current: print(current.data, end=" -> " if current.next else "") current = current.next print() def insert_at_beginning(self, data): new_node = Node(data) new_node.next = self.head self.head = new_node # Example Usage linked_list = LinkedList() node1 = Node(1) node2 = Node(2) node3 = Node(3) linked_list.head = node1 node1.next = node2 node2.next = node3 print("Initial List:") linked_list.print_list() linked_list.insert_at_beginning(0) print("\nList after inserting at the beginning:") linked_list.print_list() linked_list2 = LinkedList() print("\nEmpty List:") linked_list2.print_list()
🌐
Statistics Globe
statisticsglobe.com › home › python programming language for statistics & data science › create empty linked list node in python (example)
Create Empty Linked List Node in Python (Example) | Blank Data
December 30, 2023 - In a linked list, each node plays a crucial role in holding data, as well as establishing a connection to the next node in the list. To represent these core elements, we will define two attributes within the LLNode class, data and next. We will use the __init__ constructor to initialize them with the object instance called as self. class LLNode: # create linked list node class def __init__(self, data = None, next = None): self.data = data self.next = next
🌐
Quora
quora.com › How-do-you-create-an-empty-node-in-a-linked-list-and-then-add-data-to-it
How to create an empty node in a linked list, and then add data to it - Quora
Answer: Allocate a node. How do you declare the node is empty? Unless the node will be assigned data as part of its allocation process, then the node needs a value that signifies “empty” or include a flag that denotes an empty node. The node isn’t linked into anything when its created.
Find elsewhere
🌐
PythonForBeginners
pythonforbeginners.com › home › linked list in python
Linked List in Python - PythonForBeginners.com
April 27, 2021 - The following figure depicts a linked list having three nodes. ... We can see that the next field of the last node points to None and the reference Head points to the first Node. An empty linked list will be a linked list having its head pointer pointing to None.
🌐
Educative
educative.io › answers › how-to-create-a-linked-list-in-python
How to create a Linked List in Python
When the list is empty, the head pointer points to null. Original Python does not ship with a built-in linked list data structure like the one seen in Java. Let’s see how we can create our own implementation of a standard class-based singly linked list in Python.
🌐
DataCamp
datacamp.com › tutorial › python-linked-lists
Python Linked Lists: Tutorial With Examples | DataCamp
June 2, 2026 - We will start by initializing the linked list: class LinkedList: def __init__(self): self.head = None # Initialize head as None · By setting self.head to None, we are stating that the linked list is initially empty and that there are no nodes in the list to point to.
🌐
Medium
medium.com › @uppert83 › linked-lists-in-python-implementation-88ae726cf639
Linked Lists in Python Implementation | by Worash Abocherugn | Medium
December 14, 2023 - Printing a linked list in Python involves navigating through the list and accessing the data stored in each node. This traversal begins from the head of the list and continues until the end, printing the value or any relevant information associated with each node during the iteration. # print list => print_list def print_list(self): if self.head is None: print("the list is empty, please add some") temp = self.head print("the length of the list is:", self.length) while temp is not None: print("Node value is: ", temp.value) temp = temp.next
🌐
Built In
builtin.com › data-science › python-linked-list
An Introduction to Python Linked List and How to Create One
In this instance, we start with two attributes: Head: The first node in the linked list. Count: The number of nodes in the linked list. We’ll first create an empty head so that we start with an empty linked list.
🌐
Better Programming
betterprogramming.pub › how-to-create-a-linked-list-in-python-aaf2796e0dd6
How to Create a Linked List in Python | Better Programming
January 6, 2022 - Next, you will learn how to implement a linked list in Python. In addition, you will implement useful linked list methods, such as the insertion and deletion of nodes. Let’s get started. Let’s create a base class to represent a linked list. A linked list object only needs to know from where the list starts.
🌐
StrataScratch
stratascratch.com › blog › python-linked-lists
Creating a Python Linked List Step by Step - StrataScratch
November 7, 2025 - Create a new node with the given data. Point the new node's `next` to the current `head` (the previous top item). Update `head` to the new node. Now it's the top of the stack. This takes constant time `(O(1))` because you're just updating pointers, ...
🌐
CodingNomads
codingnomads.com › data-structure-linked-list-python
Linked Lists in Python
Create a new node with the new data to be added. Set the next reference of the new node to be the head of the list. Set the head of the list to be the new node. This works even if the list is empty since head == None in that case.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › how-to-create-linked-list
How to create linked list? - GeeksforGeeks
March 21, 2024 - # Define the Node class class Node: def __init__(self, data=None): self.data = data # The data stored in the node self.next = None # Pointer to the next node # Initialize the head of the list head = None # Create new nodes and add them to the list for i in range(1, 6): new_node = Node(i) # Replace i with the actual data # Link the nodes if head is None: # The list is empty, so the new node is the # head of the list head = new_node else: # The list is not empty, traverse the list to # find the last node temp = head while temp.next is not None: temp = temp.next # Now temp points to the last node, link the # new node temp.next = new_node # Print the list temp = head while temp is not None: print(temp.data, end=" ") temp = temp.next print()
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-linked-list
Python Linked List - GeeksforGeeks
December 11, 2025 - The first node is called the head node and we can traverse the whole list using this head and next links. We have created a Node class in which we have defined a __init__ function to initialize the node with the data passed as an argument and a reference with None because if we have only one node then there is nothing in its reference.