leetcode 【 Add Two Numbers 】 python 实现

题目

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

代码:oj测试通过 Runtime: 171 ms

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6
 7 class Solution:
 8     # @return a ListNode
 9     def addTwoNumbers(self, l1, l2):
10         if l1 is None:
11             return l2
12         if l2 is None:
13             return l1
14
15         dummyhead = ListNode(0)
16         p = ListNode(0)
17         dummyhead.next = p
18
19         jinwei = 0
20         while l1 is not None and l2 is not None:
21             curr_total = l1.val + l2.val + jinwei
22             l1 = l1.next
23             l2 = l2.next
24             curr_digit = curr_total % 10
25             jinwei = curr_total / 10
26             curr_node = ListNode(curr_digit)
27             p.next = curr_node
28             p = p.next
29
30         if l1 is not None:
31             while l1 is not None:
32                 curr_total = l1.val + jinwei
33                 l1 = l1.next
34                 curr_digit = curr_total % 10
35                 jinwei = curr_total / 10
36                 curr_node = ListNode(curr_digit)
37                 p.next = curr_node
38                 p = p.next
39         if l2 is not None:
40             while l2 is not None:
41                 curr_total = l2.val + jinwei
42                 l2 = l2.next
43                 curr_digit = curr_total % 10
44                 jinwei = curr_total / 10
45                 curr_node = ListNode(curr_digit)
46                 p.next = curr_node
47                 p = p.next
48
49         if jinwei == 1:
50             curr_node = ListNode(1)
51             p.next = curr_node
52
53         return dummyhead.next.next

思路

就是加法运算 注意两条链表上所有值计算过后 是否有进位;如果有进位 需要再处理一下。

时间: 2024-11-08 22:52:44

leetcode 【 Add Two Numbers 】 python 实现的相关文章

leetcode add two numbers python

# 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 &q

LeetCode: 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 ->

LeetCode——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 ->

leetcode——Add Two Numbers 两个链表表示的正整数对其求和(AC)

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 ->

LeetCode: Add Two Numbers 解题报告

Add Two NumbersYou 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

Leetcode 解题 Add Two Numbers Python

原题: 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) Out

[LeetCode] 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 ->

[LeetCode] Add Two Numbers II 两个数字相加之二

You are given two linked lists representing two non-negative numbers. The most significant digit comes first 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 con

[LeetCode] 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 ->

leetcode Add Two Numbers(对指针的一些基本操作)

1 ListNode *ptr,*l; 2 l = new ListNode(0);//这才是正确的赋值姿势 3 ptr = l;//赋给的是地址 4 int up = 0,fg1 = 0,fg2 = 0; 5 //cout<<"r"<<endl; 6 while(1) 7 { 8 if(fg1 && fg2) break; 9 int a,b; 10 if(fg1) a = 0; 11 else a = l1 -> val; 12 if(