Time Limit Exceeded


  • 0
    W

    This gives Time Limit Exceeded error for Last executed input: {1,2} (I omitted code for more than two listnodes)Does anyone know what's going on here?

    public class Solution {
    public static ListNode swapPairs(ListNode head) {
    	if(head == null) {
    		return null;
    	}
    	if(head.next == null) {
    		return head;
    	}
    	ListNode ret;
    	
    	if(head.next.next == null) {
    		ret = head;
    		head.next.next = head;
    		return ret.next;
    	}
    	return null;
    }

  • 0
    K

    your list is circular
    if(head.next.next == null) {
    ret = head;
    head.next.next = head;
    return ret.next;
    }
    else{
    head->next = 0;
    }


Log in to reply
 

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