Why my code get a "Runtime error" in quesion add two numbers


  • 0
    U
     class Solution
    {
    public:
    
    	static ListNode*  addTwoNumbers(ListNode* l1, ListNode* l2);
    };
    
    
    ListNode* Solution::addTwoNumbers(ListNode* l1, ListNode* l2) {
    	ListNode l3(0);
    	while(1)
    	{
    		static ListNode* l4 = &l3;
    		static int decade = 0;
    		int unit = (l1->val + l2->val) % 10;
    		l4->val = (decade + unit)%10;
    		decade = (decade + l1->val + l2->val) / 10;
    		if (l1->next != NULL) {
    			l2 = l2->next;
    			l1 = l1->next;
    			l4->next = new ListNode(0);
    			l4 = l4->next;
    		}
    		else
    		{
    			break;
    		}
    	} 
    	return &l3;
    }
    

    I test it on VS2013 with the default test case, and it's working pretty well. But when I copy the code to leetcode I get a "runime error" .


Log in to reply
 

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