• LeetCode链表练习


    题目来自于:https://leetcode-cn.com/problems/add-two-numbers

    Python

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, val=0, next=None):
    #         self.val = val
    #         self.next = next
    class Solution:
        def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
            ls = ListNode()
            ls_copy = ls
            carry = 0
            while l1 or l2:
                if not l1:
                    sum_bit = l2.val + carry
                    l2 = l2.next
                elif not l2:
                    sum_bit = l1.val + carry
                    l1 = l1.next
                else:
                    sum_bit = l2.val + l1.val + carry
                    l1 = l1.next
                    l2 = l2.next
                
                if sum_bit >= 10:
                    ls_copy.next = ListNode(sum_bit-10)
                    carry = 1
                else:
                    ls_copy.next = ListNode(sum_bit)
                    carry = 0
                ls_copy = ls_copy.next
            if carry == 1:
                ls_copy.next = ListNode(1)
            return ls.next

    C语言

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    
    
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
        struct ListNode * ls=(struct ListNode*)malloc(sizeof(struct ListNode));//创建一个头结点
        struct ListNode * ls_copy=ls;//声明一个指针指向头结点,用于遍历链表
        int carry = 0;
        int sum_bit;
        while (l1 || l2){
            if (l1==NULL){
                sum_bit = l2->val + carry;
                l2 = l2->next;
            }else if (l2==NULL){
                sum_bit = l1->val + carry;
                l1 = l1->next;
            }else{
                sum_bit = l1->val + l2->val + carry;
                l1 = l1->next;
                l2 = l2->next;
            }
            
            struct ListNode * c=(struct ListNode*)malloc(sizeof(struct ListNode));
            if (sum_bit >= 10){
                c->val = sum_bit-10;
                carry = 1;
            }else{
                c->val = sum_bit;
                carry = 0;
            }
            c->next = NULL;
            ls_copy->next = c;
            ls_copy = ls_copy->next;
        }
        if (carry == 1){
            struct ListNode * c=(struct ListNode*)malloc(sizeof(struct ListNode));
            c->val = 1;
            c->next = NULL;
            ls_copy->next = c;
        }
        return ls->next;
    }

    思路

    参考于评论区的大佬。

    1.首先创建一个空的链表,用于存储计算的结果。

    2.如果待计算的l1和l2中有一个未到末尾,则执行计算。

        2.1 如果l1和l2都未到末尾,则直接将当前值相加,若其中一个已到达末尾,则不加其数值。

        2.2 将l1和l2向后移动一位,如果已到末尾则不移动。

        2.3 根据求和值是否大于等于10,酌情进位。

    3.存储求和结果。

    4.如果循环结束时进位为1,则在结果最后再补一位1.

  • 相关阅读:
    结对编程
    四则运算
    认清自我,创造未来!!!
    浅谈GIT
    典型用户及用例故事
    结对编程 四则运算
    四则运算
    《未来的程序员》
    java项目命名规范
    四则运算测试
  • 原文地址:https://www.cnblogs.com/dingdangsunny/p/15225020.html
Copyright © 2020-2023  润新知