The second day after leetcode

Keywords: C++ Java Python

Title Description

Two non empty linked lists are given to represent two non negative integers. Their respective digits are stored in reverse order, and each node can only store one digit.

If we add the two numbers together, we will return a new linked list to represent their sum.

You can assume that neither of these numbers starts with 0 except for the number 0.

Example:

Input: (2 - > 4 - > 3) + (5 - > 6 - > 4)
Output: 7 - > 0 - > 8
 Reason: 342 + 465 = 807

The first is c++.

At the beginning, the c + + version of java was officially solved. The code is as follows

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* dummyHead = new ListNode(0);
        ListNode* p = l1;
        ListNode* q = l2;
        ListNode* curr = dummyHead;
        int carry = 0;
        while (p != 0 || q != 0) {
            int x = (p != 0) ? p->val : 0;
            int y = (q != 0) ? q->val : 0;
            int sum = carry + x + y;
            carry = sum / 10;
            curr->next = new ListNode(sum % 10);
            curr = curr->next;
            if (p != 0) p = p->next;
            if (q != 0) q = q->next;
        }
        if (carry > 0) {
            curr->next = new ListNode(carry);
        }
        return dummyHead->next;
    }
};

The idea is to apply for a new linked list space for storage, then transfer the linked list separately, then judge the relationship between the value of the linked list and 0, and return, finally sum, then% 10 takes the remainder, finally judge the relationship between the remainder and 0, and then return

Another big guy solution

Is to apply for the space of two linked lists, and then if the linked list is not empty to traverse and add, finally judge the relationship between the linked list and the remainder and 9, and finally return to the second linked list space.

The code is as follows

class Solution 
{
public:
   
        ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) 
{
        ListNode* list_head= new ListNode(0);
       
 ListNode* list_node=list_head;
        
while(1)
        
{
            int sum=list_node->val;
       
 if(l1)
       
 {
            sum+=l1->val;
            l1=l1->next;
        }
        
if(l2)
       
 {
            sum+=l2->val;
            l2=l2->next;
        }
     
   list_node->val=sum%10;
        if(l1||l2||sum>9)
        
{
            list_node->next=new ListNode(sum/10);
            
list_node=list_node->next;
        }
       
 else{
            break;
        }
    }
   
   return list_head;
    }
};

Finally, I'll go down with python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        add_num = 0
        new_list = ListNode(0)
        cur = new_list
        cur1 = l1
        cur2 = l2
        while cur1 or cur2:
            if cur1 and cur2:
                value = cur1.val + cur2.val + add_num
            elif cur1:
                value = cur1.val + add_num
            elif cur2:
                value = cur2.val + add_num
                
            cur.next = ListNode(value % 10)
            add_num = 0
            if value > 9:
                add_num = 1
            cur = cur.next
            if cur1:
                cur1 = cur1.next
            if cur2:
                cur2 = cur2.next
        if add_num:
            cur.next = ListNode(add_num)
            cur = cur.next
        return new_list.next

Posted by beboo002 on Tue, 03 Dec 2019 03:02:43 -0800