Stuck in TLE, Python


  • 0
    P

    Can anyone explain to me i am getting a Time Limit Exceeded (TLE) error on the below code?
    And i am sure its not stuck in the while loop.

    '''
    class Solution(object):
    def swapPairs(self, head):
    """
    :type head: ListNode
    :rtype: ListNode
    """
    if not head or not head.next:
    return head

        n1, n2 = head.next, head
        head = head.next
        while n2.next and n2.next.next:
            temp = n2.next
            n2.next = n1
            n1.next = temp
            n1 = temp
            n2 = temp.next
        n2.next = n1
        n1.next = None
    
        return head
    

    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.