Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将两个单独链接的列表添加到一起?_Python_Linked List_Nodes_Singly Linked List - Fatal编程技术网

Python 如何将两个单独链接的列表添加到一起?

Python 如何将两个单独链接的列表添加到一起?,python,linked-list,nodes,singly-linked-list,Python,Linked List,Nodes,Singly Linked List,我试图编写一个Python函数,将两个链表添加到一起。每个节点包含一个潜在大整数的一个数字,最低有效数字排在第一位 Ex函数:add_linked_list_integer(a,b)-其中a和b是单独链接的列表,其每个节点包含一个正整数的一位数字 Ex问题:617+295=912将在链表中表示为(7->1->6)+(5->9->2)=(2->1->9) 我提供了一个基本的ListNode类,以及用于打印和创建整数链表的示例函数 class ListNode: '''Simple node

我试图编写一个Python函数,将两个链表添加到一起。每个节点包含一个潜在大整数的一个数字,最低有效数字排在第一位

Ex函数:add_linked_list_integer(a,b)-其中a和b是单独链接的列表,其每个节点包含一个正整数的一位数字

Ex问题:617+295=912将在链表中表示为(7->1->6)+(5->9->2)=(2->1->9)

我提供了一个基本的
ListNode
类,以及用于打印和创建整数链表的示例函数

class ListNode:
    '''Simple node for singly-linked list with _value and _next fields'''
    def __init__(self, value, next=None):
        '''Create a new node, with _value field and optional _next node pointer'''
        self._value = value
        self._next = next

def print_helper(l):
    '''Prints the value of the integer represented by the linked-list l, without trailing carriage return'''
    if l:
        if (l._value < 0) or (l._value > 9):
            raise Exception('digit out of range')
        print_helper(l._next)
        print(l._value, end="")

def print_linked_list_integer(l):
    '''Prints the value of the integer represented by the linked-list l, with trailing carriage return'''
    print_helper(l)
    print()

def create_linked_list_integer(i):
    '''Returns the linked-list representation of the integer i, least-significant digit first'''
    result = ListNode(i % 10)
    if i >= 10:
        result._next = create_linked_list_integer(i // 10)
    return result

def add_linked_list_integers(a, b):
    '''Return the sum of two integers represented as linked lists

只需执行等效的10进制加法。这意味着在数字上循环

from itertools import zip_longest

def add_linked_list_integers(xs, ys):
    carry = 0
    result = []

    for x, y in zip_longest(xs, ys, fillvalue=0):
        s = x + y + carry
        carry = s // 10
        result.append(s % 10)

    if carry > 0:
        result.append(carry)

    return result
示例运行:

>>> add_linked_list_integers([7, 1, 6], [5, 9, 2])
[2, 1, 9]

>>> to_list = lambda y: [int(x) for x in reversed(str(y))]
>>> to_int  = lambda xs: int(''.join(str(x) for x in xs)[::-1])

>>> a, b = 3192097619, 999999998472534892

>>> a + b
1000000001664632511

>>> to_int(add_linked_list_integers(to_list(a), to_list(b)))
1000000001664632511
def添加链接列表整数(a,b):
''返回表示为链表的两个整数之和''
pre_head=ListNode(-1)
进位=0
压头=预压头
a不是无,b不是无:
数字=(a._值+b._值+进位)%10
进位=(a._值+b._值+进位)//10
head.\u next=列表节点(数字)
头=头。\下一步
a=a.\U下一步
b=b.\u下一步
虽然a不是无:
数字=(a._值+进位)%10
进位=(a._值+进位)//10
head.\u next=列表节点(数字)
头=头。\下一步
a=a.\U下一步
b不是无:
数字=(b._值+进位)%10
进位=(b._值+进位)//10
head.\u next=列表节点(数字)
头=头。\下一步
b=b.\u下一步
如果携带!=0:
head.\u next=ListNode(进位)
返回pre_头。\u下一步

这就是我要做的

问题是什么?@MateenUlhaq我不确定如何添加它们。我怎样才能添加两个这样的列表?
>>> add_linked_list_integers([7, 1, 6], [5, 9, 2])
[2, 1, 9]

>>> to_list = lambda y: [int(x) for x in reversed(str(y))]
>>> to_int  = lambda xs: int(''.join(str(x) for x in xs)[::-1])

>>> a, b = 3192097619, 999999998472534892

>>> a + b
1000000001664632511

>>> to_int(add_linked_list_integers(to_list(a), to_list(b)))
1000000001664632511