Cannot pass the large test case -> run time error


  • 0
    O

    This problem is quite similar to the one in Cracking the coding interview -> check palindrome linked-list.
    Start from the mid, return the next node each time.

    Can pass normal cases, no idea why cannot pass the large one..

    Submission Result: Runtime Error

    Last executed input: {3,2,3,3,3,1,3,1,3,3,1,1,3,3,2,1,1,1,1,2,1,1,2,1,2,1,3,2,...........

    public class Solution {
        public static void reorderList(ListNode head) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
    		int len = list_len(head);
    		if(len <= 2){
    			return;
    		}
    		else{
    			reorder_helper(head, 1, len);
    		}
            
        }
    	
    	public static ListNode reorder_helper(ListNode head, int level, int len){
    		ListNode temp;
    		ListNode current;
    		ListNode temp2;
    		if(level == len / 2){
    		    if(len % 2 == 0){
    				temp = head.next.next;
    				head.next.next = null;
    				return temp;
    			}
    			else{
    				temp = head.next.next;
    				temp2 = head.next.next.next;
    				temp.next = head.next;
    				head.next.next = null;
    				head.next = temp;
    				return temp2;
    			}
    		}
    		current = reorder_helper(head.next, level + 1, len);
    		temp2 = current.next;
    		temp = head.next;
    		head.next =  current;
    		current.next = temp;
    		return temp2;
    	}
    	public static int list_len(ListNode head){
    		if(head == null){
    			return 0;
    		}
    		else{
    			return list_len(head.next) + 1;
    		}
    	}
    }

  • 0
    This post is deleted!

  • 1
    J

    I have the same problem. I run the file locally and it can pass the normal cases. But when I run on leetcode online judge, I got the same error.

    ubmission Result: Runtime Error

    Last executed input: {3,2,3,3,3,1,3,1,3,3,1,1,3,3,2,1,1,1,1,2,1,1,2,1,2,1,3,2,...........

    Below is my code. Would you please help and see what is wrong? Thanks.

    public class Solution {
        public void reorderList(ListNode head) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            // Just return if the list is null or has 1 or 2 nodes. 
            if (head == null || head.next == null || head.next.next == null) {
                return;
            }
            //The list has >= 3 nodes. Reverse the second half of the inputListNode.
            ListNode secondHalf = reverseList(BreakHalf(head));
            
            // Re-wire the ListNode reference.
            ListNode firstHalf = head;
            ListNode tmp1 = null;
            ListNode tmp2 = null;
            while (firstHalf != null) {
                tmp1 = firstHalf.next;
                tmp2 = secondHalf.next;
                firstHalf.next = secondHalf;
                if (tmp1 != null) {
                    secondHalf.next = tmp1;   
                }
                firstHalf = tmp1;
                secondHalf = tmp2;
            }
        }
        
        ListNode reverseList(ListNode list) {
            if (list == null) {
                return null;
            }
            if (list.next == null) {
                return list;
            }
            ListNode secondElem = list.next;
            ListNode reverseRest = reverseList(secondElem);
            secondElem.next = list;
            return reverseRest;
        }
        
        // Break the list into 2 half. Return the first node of the second half.
        ListNode BreakHalf(ListNode list) {
            // return null if the list is null or has 1 or 2 nodes only.
            if (list == null || list.next == null || list.next.next == null) {
                return list;
            }
            ListNode step1 = list;
            ListNode step2 = list.next;
            ListNode secondHalfHead = null;
            while (step2.next != null && step2.next.next != null) {
                step2 = step2.next.next;
                step1 = step1.next;
            }
            secondHalfHead = step1.next;
            step1.next = null;
            return secondHalfHead;
        }
    }

  • 0
    J

    need to use iterative method with O(1) space.


Log in to reply
 

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