LeetCode刷题 | NO.002

原题:

You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.

给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

思考:对应位数相加,以及要考虑是否有进位,并且要考虑两个链表的各自的长度。

使用变量来跟踪进位,并从包含最低有效位的表头开始模拟逐位相加的过程。

刚好最近在复习数据结构,以下链表(C语言实现)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {

    struct ListNode* p;
    struct ListNode* head;
    struct ListNode* q;
    struct ListNode* pre;//指向前向的指针
    int c=0;

    if(l1 == NULL) return l2;
    if(l2 == NULL) return l1;

    head = l1;
    p = l1;
    while(l1 && l2)
    {
        pre = p;
        p->val = (l1->val + l2->val +c);
        c = p->val/10;
        p->val %=10; 
        l1 = l1->next;
        l2 = l2->next;
        p = p->next;
    }
    if(!l1 && !l2)//l1 和 l2 相加数字位数相等
    {
        if(c)
        {
            q = (struct ListNode *)malloc(sizeof(struct ListNode));
            q->val = c;
            q->next = NULL;
            pre->next = q;

        }

    }
    else if(l2)
    {
        addtail(pre,l2,c);
    }
    else if(l1)
    {
        addtail(pre,l1,c);
    }
    return head;
}

void addtail(struct ListNode *pre,struct ListNode *r ,int c)
{
    struct ListNode *q;
    pre->next = r;
    q = pre->next;
    while(q)
    {
        q->val = q->val + c;
        c = q->val/10;
        q->val %= 10;
        pre = q;
        q = q->next;

    }
    if(c)
    {
        q = (struct ListNode *)malloc(sizeof(struct ListNode));
        q->val = c;
        q->next = NULL;
        pre->next = q;

    }

}

 选择其中一个链表作为最后要返回的结果链表,设置一个头指针始终指向该链表的头结点。设置一个用来移动索引的指针还有一个用来指向当前位置的前向指针。

考虑到:两个链表是否为空情况,两个链表长度相等以及两个链表长度不等的情况。

可以在草稿上画个图会更明白一些。

以下用python描述

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

class Solution:
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if l1 == None:
            return l2
        if l2 == None:
            return l1

        carry = 0
        p = head = ListNode(0)
        #定义带有头结点的空链表,head为头指针,p用于遍历,carry为记录进位情况
        while l1 and l2:
            p.next = ListNode(l1.val + l2.val + carry)
            carry = p.next.val // 10
            p.next.val %= 10

            l1 = l1.next
            l2 = l2.next
            p = p.next
        res = l1 or l2
        while res:
            p.next = ListNode(res.val + carry)
            carry = p.next.val // 10
            p.next.val %= 10
            p = p.next
            res = res.next
        if carry:
            p.next = ListNode(1)
        return head.next



猜你喜欢

转载自blog.csdn.net/qq_35924276/article/details/81293857
今日推荐