I am trying to solve a question called merge of two sorted linked list, but getting time limit exceeded Error please someone help me out guys.

Joined
Jan 16, 2023
Messages
1
Reaction score
0
#Python Code
#Code for merging the two sorted linked list
class Solution(object):
def mergeTwoLists(self, list1, list2):
"""
:type list1: Optional[ListNode]
:type list2: Optional[ListNode]
:rtype: Optional[ListNode]
"""
ans = tail = ListNode()
if (list1.val<list2.val):
ans = list1
else:
ans = list2
tail = ans
while (list1 and list2):
if(list1.val<list2.val):
tail.next=list1
tail=tail.next
list1=list1.next
else:
tail.next=list2
tail=tail.next
list2=list2.next
if(list1!=None):
tail.next=list1
if(list2!=None):
tail.next=list2
return ans
 
Joined
May 11, 2022
Messages
61
Reaction score
6
you need to use code tags to make your code readable. highlight your spaced out code and then click the </> button.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,335
Latest member
Tommiesal

Latest Threads

Top