两个单链表生成相加链表

两个单链表生成相加链表

题目

两个单链表,每个节点保存的数据0~9,表示一个数字,如
9->3->7 表示937
实现两个链表相加,如
9->3->7 + 6->3 = 1->0->0->0

链表及基本操作

import random


class Node():
    def __init__(self, val):
        self.val = val
        self.next = None


def reverse_linklist(head):
    prev = None
    cur = head
    while cur:
        next = cur.next
        cur.next = prev
        prev = cur
        cur = next

    return prev


def linklist_to_string(head):
    s = ''
    cur = head
    while cur:
        s += str(cur.val)
        cur = cur.next

    return s


def make_rand_list(count1):
    l = []
    for i in range(count1):
        x = random.randint(0, 9)
        if i == 0:
            x = random.randint(1, 9)
        l.append(x)

    return l


def make_linklist(l):
    head, tail = None, None
    for x in l:
        node = Node(x)
        if not tail:
            head = node
            tail = node
        else:
            tail.next = node
            tail = node

    return head

思路1:使用栈

实现1

def add_linklist1(head1, head2):
    stack1 = []
    stack2 = []

    p = head1
    while p:
        stack1.append(p)
        p = p.next

    p = head2
    while p:
        stack2.append(p)
        p = p.next

    carry = 0
    head = None
    while stack1 and stack2:
        n1 = stack1.pop().val
        n2 = stack2.pop().val
        x = n1 + n2 + carry
        carry = x // 10
        node = Node(x % 10)
        node.next = head
        head = node

    while stack1:
        n1 = stack1.pop()
        x = n1.val + carry
        carry = x // 10
        node = Node(x % 10)
        node.next = head
        head = node

    while stack2:
        n2 = stack2.pop()
        x = n2.val + carry
        carry = x // 10
        node = Node(x % 10)
        node.next = head
        head = node

    if carry:
        x = carry
        node = Node(x % 10)
        node.next = head
        head = node

    return head

实现2

def add_linklist2(head1, head2):
    stack1 = []
    stack2 = []

    p = head1
    while p:
        stack1.append(p)
        p = p.next

    p = head2
    while p:
        stack2.append(p)
        p = p.next

    carry = 0
    head = None
    while stack1 or stack2 or carry:
        n1 = stack1.pop().val if stack1 else 0
        n2 = stack2.pop().val if stack2 else 0
        x = n1 + n2 + carry
        carry = x // 10
        node = Node(x % 10)
        node.next = head
        head = node

    return head

思路2:反转链表

def add_linklist3(head1, head2):
    reversed_head1 = reverse_linklist(head1)
    reversed_head2 = reverse_linklist(head2)

    p1, p2 = reversed_head1, reversed_head2
    carry = 0
    head = None
    while p1 or p2 or carry:
        n1 = p1.val if p1 else 0
        n2 = p2.val if p2 else 0
        x = n1 + n2 + carry
        carry = x // 10
        node = Node(x % 10)
        node.next = head
        head = node

        if p1: p1 = p1.next
        if p2: p2 = p2.next

    head1 = reverse_linklist(reversed_head1)
    head2 = reverse_linklist(reversed_head2)
    return head

测试

def test_add_linklist(count1, count2):
    datas1 = make_rand_list(count1)
    datas2 = make_rand_list(count2)

    head1 = make_linklist(datas1)
    head2 = make_linklist(datas2)

    s1 = linklist_to_string(head1)
    s2 = linklist_to_string(head2)

    result1 = add_linklist1(head1, head2)
    sresult1 = linklist_to_string(result1)
    if int(s1) + int(s2) != int(sresult1):
        raise Exception('error')

    result2 = add_linklist2(head1, head2)
    sresult2 = linklist_to_string(result2)
    if int(s1) + int(s2) != int(sresult2):
        raise Exception('error')

    result3 = add_linklist3(head1, head2)
    sresult3 = linklist_to_string(result3)
    if int(s1) + int(s2) != int(sresult3):
        raise Exception('error')


if __name__ == '__main__':
    test_add_linklist(5, 5)
    test_add_linklist(5, 6)
    test_add_linklist(6, 5)
    test_add_linklist(150, 150)
    test_add_linklist(149, 150)
    test_add_linklist(150, 151)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值