Runtime error errors encountered on Leetcode

Source: Internet
Author: User

Today on the Leetcode to do a single link table to find the intersection of the algorithm question (160), the following error occurred when submitting:


The submitted code is as follows:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */<pre name= "code" class= "CPP" >/* double pointer to the intersection of single linked table * *
<pre name= "code" class= "CPP" >struct listnode *getintersectionnode (struct listnode *heada, struct ListNode *headB) {
    struct listnode *pa=heada,*pb=headb;
    int la,lb,n=0;
    if (pa==null| | Pb==null) return
      NULL;
    while (PA)
    {
        la++;
        pa=pa->next;
    }
    while (PB)
    {
        lb++;
        pb=pb->next;
    }
    if (la<=lb)
    {
        N=lb-la;
        Pa=heada;
        pb=headb;
        while (n)
        {
            pb=pb->next;
            n--
        }
    }
    else
    {
        n=la-lb;
        Pa=heada;
        pb=headb;
        while (n)
        {
            pa=pa->next;
            n--
        }
    }
    while (PA!=PB)
    {
       pa=pa->next;
       pb=pb->next;
    }
    return PA;
}
Carefully looking for a long time also did not find the program logic errors, so I can not understand the solution, so began to study runtime error This problem, on the http://www.webopedia.com/TERM/R/runtime_error.html found the following definition:

Runtime Error
by Vangie Beal

(Run´tīm Er´&r) (n.) An error that occurs during the execution of a. In contrast, compile-time errors occur while a is being compiled. Runtime errors indicate bugs in the "program" or problems that the designers had the anticipated does nothing about. For example, running out of memory'll often cause a runtime error.

Note This runtime errors differ from bombs or crashes in so you can often recover gracefully error. Roughly translated as follows:

Rumtime error (Run-time errors) is an error that occurs during the execution of a program. In contrast to the Compile-time errors (compile-time error), it occurs during program compilation. Runtime errors indicates that there are some vulnerabilities and problems in the program, and programmers can anticipate these errors but not do anything. For example, a memory overflow usually causes runtime error.

It should be noted that runtime errors and bombs or crashes (program crashes) are different, and you can usually gracefully recover from the former.

In addition, in C + + to find the Run_time this class (http://www.cplusplus.com/reference/stdexcept/runtime_error/):


You can see that there are four kinds of errors will cause runtime_error, you can click on the link above to view.

Know the runtime error mechanism back to my program, and finally found that the problem is variable initialization:

    struct ListNode *pa=heada,*pb=headb;
    int la,lb,n=0;
You would have wanted to initialize la,lb,n to 0, but the code above actually declares only LA and LB, and it's not initialized, so the runtime's value is a random number, causing a program error.

Change the above code to read:

    struct ListNode *pa=heada,*pb=headb;
    int la=0,lb=0,n=0;
Submitted through.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.