-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.py
More file actions
43 lines (35 loc) · 960 Bytes
/
Copy path2.py
File metadata and controls
43 lines (35 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <wuyadong311521@gmail.com>']
# 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
"""
p1 = l1
p2 = l2
carry = 0
parent = ListNode(0)
p3 = parent
while p1 or p2:
v1 = p1.val if p1 else 0
v2 = p2.val if p2 else 0
p3.val = (v1 + v2 + carry) % 10
carry = (v1 + v2 + carry) / 10
if p1:
p1 = p1.next
if p2:
p2 = p2.next
if p1 or p2:
p3.next = ListNode(0)
p3 = p3.next
if carry > 0:
p3.next = ListNode(carry)
return parent