Leetcode Question 2 Two-Number Addition C Language Edition

Keywords: Java less C

Because of the clever operation of the counselors in our institute (yes, the Brother College of Communications and Communications). (We are required to brush the button without learning the data structure.)
I plan ahead of time, because I have learned a little knowledge of the list by myself, so I want to try to find a question to practice, so I chose the second question (never easy to touch the middle question later.)
After many additions and bug fixes, after the baptism of agile development and three and a half days of hard work, I finally made this problem.
Looking at the results, I found that it was not very ideal. Once again, I would like to share my ideas and experience of improvement with you, and also remind you to take less detours.


First of all, the code is enclosed:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
	struct ListNode *Apply(int x){
		struct ListNode * p,*q;
		p = (struct ListNode *)malloc(sizeof(struct ListNode));
		if (x < 10) {
		p->val = x;
		p->next = NULL;
		return(p);		
		}
		else {
			p->val = x - 10;
			q = (struct ListNode *)malloc(sizeof(struct ListNode));
			q->val = 1;
			q->next = NULL;
			p->next = q;
			return(p);
		}
	}
	//Initialize nodes (create table headers)
	struct ListNode *InitList(){
		struct ListNode *head;
		head = (struct ListNode *)malloc(sizeof(struct ListNode));
        head->val = 0;
		head->next = NULL;
		return(head);
	}

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
	    struct	ListNode* head, *p, *q,*m,*n;
		p = l1;
  		q = l2;
  		int flag = 0, temp = 0, shi = 0,flag1 = 1;
		head = m = InitList();
		while (p!=NULL && q!=NULL){
			temp = (p->val) + (q->val);
			if (flag != 1) {
				shi = temp >= 10 ? 1 : 0;
				temp = temp - 10 * (temp / 10);//Bit
				n = Apply(temp+flag);
				m->next = n;
				m = n;				//Establishment of a new linked list by tail insertion
				flag = shi;
				p = p->next;
				q = q->next;
			}
			else {
				shi = temp >= 9 ? 1 : 0;
				temp = temp + flag >= 10 ? temp+flag-10:temp+flag ;
				n = Apply(temp );
				m->next = n;
				m = n;				//Establishment of a new linked list by tail insertion
				flag = shi;
				p = p->next;
				q = q->next;
			}
		}
    
			while (p!=NULL)//l1 Not Ended l2 Ended
			{
				if (flag != 0) {
					while (flag!=0 && p!=NULL)
					{
						if (p == NULL)//********************************
						{
							n = Apply(1);
							m->next = n;
							m = n;
						}
						else {
							if (p->val > 8)
							{
								p->val = p->val + 1 - 10;
								flag = 1;
							n = Apply(p->val);
							m->next = n;
							m = n;
							p = p->next;
							}
							else {
								flag = 0;

							n = Apply(1 + p->val);
							m->next = n;
							m = n;
							p = p->next;
							}
						}				
					}
				}
				else {
					n = Apply( p->val);
					m->next = n;
					m = n;
					p = p->next;
				}
				if (p == NULL && q == NULL && m->val == 0) {    //Prevent the last 0
				
					m->next = Apply(1);
					p = NULL;
				}
			}
			
			while (q!=NULL)//l2 Not Ended l1 Ended
			{
				if (flag != 0) {
					while (flag != 0 && q != NULL)
					{
						if (q == NULL)//********************************
						{
							n = Apply(1);
							m->next = n;
							m = n;
						}
						else {
							if (q->val > 8)
							{
								q->val = q->val + 1 - 10;
								flag = 1;
								n = Apply(q->val);
								m->next = n;
								m = n;
								q = q->next;
							}
							else {
								flag = 0;
								n = Apply(1 + q->val);
								m->next = n;
								m = n;
								q = q->next;
							}
						}
					}
				}
				else {
					n = Apply(q->val);
					m->next = n;
					m = n;
					q = q->next;
				}
				if (p == NULL && q == NULL && m->val == 0) {   //Prevent the last 0
					m->next = Apply(1);
					q = NULL;
				}
			}
			if (flag != 0) {
				n = Apply(1);
				m->next = n;
				m = n;
				flag = 0;
			}
		return(head->next);
}

My own experience is scattered.

First, you can see that in order to simplify the steps of establishing link list nodes, I wrote two functions InitList() and Apply(), respectively, to establish header pointers and apply for new nodes.
2. Because I only have a list of header nodes, but the title is a list without header nodes, so I chose to do it according to the method of tail insertion, and return to head - > next when returning.

Next, let's talk about the process of improvement.
1. First of all, I solved the problem of 342+456 by setting variables: ten-bit shi, one-bit temp, and carry flag.
2. Next, the next example [1,8]+ [0] is difficult for me, because I did not take into account the different number of digits. After querying other people's answers, I found that because the neutralization condition of my own while loop is (p!=NULL & q!=NULL), after I quit the loop, if (p!=NULL), then I pointed to the l1 list. There are residual elements, that is, l1 is not finished. According to this idea, I wrote while (p!=NULL) and while (q!=NULL) to solve this problem.
3. Then, without considering carry, in the case of [2]+ [8, 9, 9, 9], the car rolled over again, so I added a flag carry mark to determine.
4. In the main cycle there is still an example of [2,1,8]+ [8,2,9,9] rollover.... Re-improvement
5. Because of the VS2017 used by myself, obviously the compiler function is not so powerful. If you encounter the application of null pointer, it will be timed out and changed again. Add null pointer judgment...
Eventually it's this long, stinky...
It's ugly, but it's clear. I think it's necessary to look at other people. How can they finish it in dozens of lines?

Finally, the C language does write very long, not as many lines as py or Java, but my C runs fast, and memory space is only 9MB, very few (the same 8ms Java uses 44MB)

Posted by mariocesar on Sun, 28 Jul 2019 01:50:28 -0700