leetcode——2

1. 题目

Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

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

给你两个链表代表两个非负数。数字以相反的顺序存储,每个节点包含一个单一的数字。加上这两个数并返回一个链表。

 

2.c++解题

 //LeetCode_Add Two Numbers
//Written by zhou
//2013.11.1

/**
 * 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) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        
        if (l1 == NULL) return l2;
        if (l2 == NULL) return l1;

        ListNode *resList = NULL, *pNode = NULL, *pNext = NULL; // resList头节点, pNode 每轮的末节点, pNext临时节点
        ListNode *p = l1, *q = l2;
        int up = 0;
        while(p != NULL && q != NULL)
        {
            pNext = new ListNode(p->val + q->val + up);
            up = pNext->val / 10;    //计算进位
            pNext->val = pNext->val % 10;   //计算该位的数字
            
            if (resList == NULL)  //头结点为空
            {
                resList = pNode = pNext;
            }
            else //头结点不为空
            {
                pNode->next = pNext;
                pNode = pNext;
            }
            p = p->next;
            q = q->next;
        }

        //处理链表l1剩余的高位
        while (p != NULL)
        {
            pNext = new ListNode(p->val + up);
            up = pNext->val / 10;    
            pNext->val = pNext->val % 10;
            pNode->next = pNext;
            pNode = pNext;
            p = p->next;
        }

        //处理链表l2剩余的高位
        while (q != NULL)
        {
            pNext = new ListNode(q->val + up);
            up = pNext->val / 10;    
            pNext->val = pNext->val % 10;
            pNode->next = pNext;
            pNode = pNext;
            q = q->next;
        }

        //如果有最高处的进位,需要增加结点存储
        if (up > 0)
        {
            pNext = new ListNode(up);
            pNode->next = pNext;
        }

        return resList;
    }

};

3. python解题

3.1

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    # @return a ListNode
    def addTwoNumbers(self, l1, l2):
        dummy, flag = ListNode(0), 0
        head = dummy
        while flag or l1 or l2: //flag 进位, node临时节点, dummy最后节点
            node = ListNode(flag)
            if l1:
                node.val += l1.val
                l1 = l1.next
            if l2:
                node.val += l2.val
                l2 = l2.next
            flag = node.val / 10
            node.val %= 10
            head.next = node  
            head = head.next  # head.next, head = node, node
        return dummy.next

3.2

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    # @return a ListNode
    def addTwoNumbers(self, l1, l2):
     if not l1: return l2
        if not l2: return l1
        dummy = ListNode(0)
        p = dummy
        flag = 0
        while l1 and l2:
            tmp = l1.val + l2.val + flag
            p.next = ListNode( tmp % 10 )
            flag = tmp / 10
            l1, l2, p = l1.next, l2.next, p.next
        if l1:
            while l1:
                tmp = l1.val + flag
                p.next = ListNode( tmp % 10 )
                flag = tmp / 10
                l1, p = l1.next, p.next
        if l2:
            while l2:
                tmp = l2.val + flag
                p.next = ListNode( tmp % 10 )
                flag = tmp / 10
                l2, p = l2.next, p.next
        if flag == 1: p.next = ListNode(flag)
        return dummy.next
     



你可能感兴趣的:(LeetCode)