Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
- I know this is not the optimal solution but still I'm not able to understand my mistake.*
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(ListNode):
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
lst =[]
while(l1!=None):
#print("I")
lst.append(l1.val)
l1 = l1.next
while (l2!=None):
#print("I")
lst.append(l2.val)
l2 = l2.next
lst.sort()
#print(lst)
l = ListNode(0)
#temp : ListNode()
temp = l
# temp.val = l.val
for i in lst:
temp.val = i
temp = temp.next
return l
error is
AttributeError: 'NoneType' object has no attribute 'val'
temp.val = i