Time out of limit?


  • 0
    L
        if(head == null)
    		return null;
    	ListNode newHead = head;
    	ListNode cur;
    	head = head.next;
    	while (head != null) {
    		cur = head;
    		head = head.next;
    		cur.next = newHead;
    		newHead = cur;
    	}
    	return newHead;

  • 0
    S

    I meet the similar problem, but I run the program in VIM, it's OK. write in C.
    I change your code like the following:

    struct ListNode* reverseList(struct ListNode* head) {
       if(head == NULL)
        return head;
    struct ListNode *newHead = head;
    struct ListNode *cur;
    head = head->next;
    newHead->next = NULL;
    while (head != NULL) {
        cur = head;
        head = head->next;
        cur->next = newHead;
        newHead = cur;
    }
    return newHead;
    

    }

    AC. Maybe initialize the new Linked List to one element.


Log in to reply
 

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