力扣_day2

两数相加

用python3更方便,有内置的ListNode。

# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

class Solution(object):
    def cycles(self,head):
        now = head
        index = 0
        while(now):
            index += 1
            now = now.next
        return index

    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        len1 = self.cycles(l1)
        len2 = self.cycles(l2)
        
        if len1 > len2:
            long_list = l1
            long_len = len1
            short_list = l2
            short_len = len2
        else:
            long_list = l2
            long_len = len2
            short_list = l1
            short_len = len1

        i = 0
        a = short_list
        b = long_list
        flag = 0
        while(i

你可能感兴趣的:(力扣,leetcode,算法,职场和发展)