# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(-1)
tail = dummy
l1head = list1
l2head = list2
while l1head and l2head:
if l1head.val <= l2head.val:
tail.next = l1head
l1head = l1head.next
else:
tail.next = l2head
l2head = l2head.next
tail = tail.next
if l1head:
tail.next = l1head
elif l2head:
tail.next = l2head
return dummy.next
Error
The 2 pointers method does not work unless very carefully planned, and it is better to solve it the easy way with a dummy variable, then move on to medium and hard problems (of which there are many variations, one of which will match the difficulty level you are looking for).