python寫演算法題:leetcode: 2. Add Two Numbers
https://leetcode.com/problems/add-two-numbers/#/description
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ rtype = ListNode(0) add=0 r=rtype while l1 and l2: val = l1.val+l2.val+add if r.next: r=r.next r.val = val%10 add = val/10 l1=l1.next l2=l2.next r.next=ListNode(0) while l1: val = l1.val+add if r.next: r=r.next r.val = val%10 add = val/10 l1=l1.next r.next=ListNode(0) while l2: val = l2.val+add if r.next: r=r.next r.val = val%10 add = val/10 l2=l2.next r.next=ListNode(0) if add>0: r=r.next r.val=add r.next = None return rtype
思路: 太簡單,程式碼都懶得重構了
相關推薦
python寫演算法題:leetcode: 2. Add Two Numbers
https://leetcode.com/problems/add-two-numbers/#/description # Definition for singly-linked list. # class ListNode(object): # def __in
LeetCode 2 — Add Two Numbers(C++ Java Python)
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes con
題解——Leetcode 2. Add Two Numbers 難度:Medium
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their
Leetcode# 2. Add Two Numbers(連結串列模擬大數演算法)
Add Two Numbers(連結串列) You are given two non-empty linked lists representing two non-negative integers. The digits are stored in re
LeetCode - 2 - Add Two Numbers
dtw ble bind ber plus binding bsp pan number 題目 URL:https://leetcode.com/problems/add-two-numbers/ 解法 沒什麽特殊的解法,按位相加即可。 註意點: 1、l1 和 l
LeetCode 2: Add Two Numbers
ret change add cal lis || div span style Tips: Tried to use a private method to simplize the code. But found that object is passed by ref
LeetCode - 2. Add Two Numbers
ont and contain nod emp list sts init pre You are given two non-empty linked lists representing two non-negative integers. The digits are
[leetcode] 2. Add Two Numbers
code eve ini emp any n-n self 申請 tno You are given two non-empty linked lists representing two non-negative integers. The digits are stor
Leetcode #2 Add two numbers
理解 contains linked cti tin arr != 情況 目的 ## 題目 >Add Two NumbersYou are given two linked lists representing two non-negative numbers. Th
leetcode:2-Add Two Numbers
另一個 plan example num 提前 除了 += 指向 pre You are given two non-empty linked lists representing two non-negative integers. The digits are stor
[LeetCode]2. Add Two Numbers鏈表相加
pre int 處理 span node new spa ont lag 註意進位的處理和節點為null的處理 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int flag = 0;
LeetCode 2.Add Two Numbers
elf output git any == != ret ber spa You are given two non-empty linked lists representing two non-negative integers. The digits are stor
Leetcode 2. Add Two Numbers (java)
style 相加進位 ret java numbers null bool ext leet 解法: class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
[leetcode][2] Add Two Numbers
plan leet pub repr put any 原來 integer exce 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative inte
leetcode-2 Add Two Numbers
single round output num spa tor repr tro 情況 You are given two non-empty linked lists representing two non-negative integers. The digits a
Leetcode 2. Add Two Numbers 兩數相加
Leetcode 2. Add Two Numbers 兩數相加 標籤: Leetcode 題目地址:https://leetcode-cn.com/problems/add-two-numbers/ 題目描述 給出兩個非空的連結串列用來表示兩個非負的整數。其中,它
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
LeetCode 2. Add Two Numbers
問題:兩個數字逆序存在兩個連結串列中,返回兩數之和的連結串列 最開始想著轉換成數字加起來再存到連結串列了,顯然,溢位了,改成一位一位操作,邊加邊存 (以前沒寫過連結串列,寫的又長又醜,最後跑的也很慢) C++: /** * Definition for singly
LeetCode-2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are store
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 ea