zoukankan      html  css  js  c++  java
  • leetcode--(2. Add Two Numbers)

    题目描述:

    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.

    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8

    翻译:对两个单向链表求和,链表头为低位,链表尾是高位

    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
        var l3 =&ListNode{}
        q:=l3
        c:=0
        for l1!=nil&&l2!=nil{
            p:=&ListNode{}
            sum:=l1.Val+l2.Val+c
            p.Val=sum%10
            q.Next=p
            q=p
            c=sum/10
            l1=l1.Next
            l2=l2.Next
    
        }
        for l1!=nil{
            p:=&ListNode{}
            sum:=l1.Val+c
            p.Val=sum%10
            q.Next=p 
            q=p 
            c=sum/10
            l1=l1.Next    
        }
        for l2!=nil{
            p:=&ListNode{}
            sum:=l2.Val+c
            p.Val=sum%10
            q.Next=p 
            q=p 
            c=sum/10
            l2=l2.Next    
        }
        for c>0{
            p:=&ListNode{}
            p.Val=c%10
            c=c/10
            q.Next=p 
            q=p 
        }
        return l3.Next
    }
  • 相关阅读:
    Python修改文件内容
    Python实现用户注册到文件
    Postman接口测试
    Linux下安装LoadRunner LoadGenerator
    Loadrunner参数化避免重复数据
    Ta-Lib用法介绍 !
    迭代器 生成器
    深入理解python多进程编程
    python多进程
    python多线程
  • 原文地址:https://www.cnblogs.com/fwdqxl/p/7396532.html
Copyright © 2011-2022 走看看