[LeetCode Training Camp 02] Detailed explanation of adding two non-empty linked lists

Table of contents

topic 

Sharing of problem-solving ideas

Sharing of problem-solving source code

 

topic 

You are given two  non-empty  linked lists representing two non-negative integers. Each of their digits is   stored  in reverse order, and each node can only store one  digit.

Please add two numbers and return a linked list representing the sum in the same form.

You can assume that neither number starts with a zero other than the number zero.

Example 1:

Input: l1 = [2,4,3], l2 = [5,6,4]
 Output: [7,0,8]
 Explanation: 342 + 465 = 807.

Example 2:

Input: l1 = [0], l2 = [0]
 Output: [0]

Example 3:

Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
 Output: [8,9,9,9,0,0,0,1]

hint:

  • The number of nodes in each linked list is in the  [1, 100] range
  • 0 <= Node.val <= 9
  • The title data guarantees that the number represented by the list does not contain leading zeros

Sharing of problem-solving ideas

  • The requirement of the topic is to sum two linked lists and return the resulting linked list.
  • My first thought was to operate on l1 and return it, but this idea was quickly overturned by me.
  • If l1 is used as the operation object, then the value of the next node of l1 will be modified every tenth, which is definitely not possible.
  • So we define a single linked list separately, and operate on this single linked list.
  • A singly linked list must have a head pointer and a tail pointer.
  • We also need to define a sum to store the result of the basic operation, and a cf to store the result of the carry operation.
  • There are three situations for the operation of sum, the normal situation (when both l1 and l2 have not reached the empty node), when l1 reaches the empty node, and when l2 reaches the empty node.
  • In order to store the operation result of each bit, a new node must be created.
  • sum%10 is the value of this node, and cf/10 is the carry value, which should be saved for the next operation.
  • The insertion of a node is similar to the tail insertion of a singly linked list, and there are two cases, so I won't say more here.
  • Finally, there is another situation that both l1 and l2 have reached the empty node, but cf is not yet zero, which means creating a new node to store this value and serve as the end node.

Sharing of problem-solving source code

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
   struct ListNode*head=NULL;
   struct ListNode*tail=NULL;
   int sum=0;
   int cf=0;
   while(l1!=NULL||l2!=NULL)
   {
       if(l1==NULL)
       {
           sum=l2->val+cf;
           l2=l2->next;
       }
       else if(l2==NULL)
       {
           sum=l1->val+cf;
           l1=l1->next;
       }
       else{
           sum=l1->val+l2->val+cf;
           l1=l1->next;
           l2=l2->next;
       }
   
   struct ListNode*sb=(struct ListNode*)malloc(sizeof(struct ListNode));
   sb->val=sum%10;
   sb->next=NULL;
   cf=sum/10;
   if(head==NULL)
   {
       head=sb;
       tail=sb;
   }
   else{
       tail->next=sb;
       tail=sb;
   }
   if(l1==NULL&&l2==NULL&&cf!=0)
   {
       struct ListNode*sb=(struct ListNode*)malloc(sizeof(struct ListNode));
       sb->val=cf;
       sb->next=NULL;
       tail->next=sb;
       tail=sb;
   }
   }
   return head;
}

 This method may be a bit complicated and redundant. If there is anything that can be improved, please feel free to enlighten me.

 

Guess you like

Origin blog.csdn.net/weixin_73534885/article/details/130119262