python链表 —— 如何计算两个单链表所代表的数之和

 题目描述:

给定两个单向链表,链表的每个节点代表一位数,计算两个数的和

方法分析:

方法一: 整数相加

分别遍历两个链表所代表的值,然后将两个整数加到一起

方法二:链表相加法

对链表中的节点直接进行相加操作,把相加的和存储到新的链表中对应的节点中,同时还要记录节点相加后的进位。

算法性能分析:

这种方法需要对两个链表遍历,因此时间复杂度为o(N),由于计算结果保存在一个新的链表中,空间复杂度为o(N)

代码实现:

1. 带头节点实现

带头结点实现
class LNode:
    def __init__(self, item):
        self.data = item
        self.next = None


def add(h1, h2):
    if h1 is None or h1.next is None:
        return h2
    if h2 is None or h2.next is None:
        return h1
    c = 0
    sum = 0
    p1 = h1.next
    p2 = h2.next
    tmp = None
    result_head = LNode(None)
    result_head.next = None
    p = result_head

    while p1 is not None and p2 is not None:
        sum = p1.data + p2.data + c
        tmp = LNode(sum % 10)
        tmp.next = None
        c = sum // 10
        p.next = tmp
        p = tmp
        p1 = p1.next
        p2 = p2.next

    if p1 is None:
        while p2 is not None:
            sum = p2.data + c
            tmp = LNode()
            tmp.next = None
            tmp.data = sum % 10
            c = sum // 10
            p.next = tmp
            p = tmp
            p2 = p2.next
    if p2 is None:
        while p1 is not None:
            sum = p1.data + c
            tmp = LNode(sum % 10)
            tmp.next = None
            c = sum // 10
            p.next = tmp
            p1 = p1.next
    if c == 1:
        tmp = LNode(1)
        tmp.next = None
        p.next = tmp
    return result_head

if __name__ == '__main__':
    i = 1
    head1 = LNode(None)
    head1.next = None
    head2 = LNode(None)
    head2.next = None
    tmp = None
    cur = head1
    add_result = None

    while i < 7:
        tmp = LNode(i+2)
        tmp.next = None
        cur.next = tmp
        cur = tmp
        i += 1
    print("i的值为:%d"%i)

    i = 9
    cur = head2
    while i > 4:
        tmp = LNode(i)
        tmp.next = None
        cur.next = tmp
        cur = tmp
        i -= 1


    print("\nhead1")
    cur = head1.next
    while cur is not None:
        print(cur.data)
        cur = cur.next

    print("\nhead2")
    cur = head2.next
    while cur is not None:
        print(cur.data)
        cur = cur.next

    add_result = add(head1, head2)
    print("\n相加后:")
    cur = add_result.next
    while cur is not None:
        print(cur.data)
        cur = cur.next

代码运行结果:

head1
3
4
5
6
7
8

head2
9
8
7
6
5

相加后:
2
3
3
3
3
9

2. 不带头结点

class LNode:
    def __init__(self, item):
        self.data = item
        self.next = None

def add(h1, h2):
    if h1 is None:
        return h2
    if h2 is None:
        return h1
    c = 0
    sum = 0
    p1 = h1
    p2 = h2
    tmp = None
    result_head = None
    p = result_head
    while p1 is not None and p2 is not None:
        sum = p1.data + p2.data + c
        tmp = LNode(sum % 10)
        tmp.next = None
        c = sum // 10
        if p is not None:
            p.next = tmp
            p = tmp
        else:
            result_head = tmp
            p = result_head
        p1 = p1.next
        p2 = p2.next

    if p1 is None:
        while p2 is not None:
            sum = p2.data + c
            tmp = LNode()
            tmp.next = None
            tmp.data = sum % 10
            c = sum // 10
            p.next = tmp
            p = tmp
            p2 = p2.next
    if p2 is None:
        while p1 is not None:
            sum = p1.data + c
            tmp = LNode(sum % 10)
            tmp.next = None
            c = sum // 10
            p.next = tmp
            p1 = p1.next
    if c == 1:
        tmp = LNode(1)
        tmp.next = None
        p.next = tmp
    return result_head

if __name__ == '__main__':
    i = 1
    head1 = None
    head2 = None
    tmp = None
    cur = head1
    add_result = None

    while i < 7:
        tmp = LNode(i+2)
        tmp.next = None
        if cur is not None:
            cur.next = tmp
            cur = tmp
        else:
            head1 = tmp
            cur = head1
        i += 1
    print("i的值为:%d"%i)

    i = 9
    cur = head2
    while i > 4:
        tmp = LNode(i)
        tmp.next = None
        if cur is not None:
            cur.next = tmp
            cur = tmp
        else:
            head2 = tmp
            cur = head2
        i -= 1

    print("\nhead1")

    cur = head1
    while cur is not None:
        print(cur.data)
        cur = cur.next

    print("\nhead2")
    cur = head2
    while cur is not None:
        print(cur.data)
        cur = cur.next

    add_result = add(head1, head2)
    print("\n相加后:")
    cur = add_result
    while cur is not None:
        print(cur.data)
        cur = cur.next

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现求两个单链表的差集,即找出在第一个链表中出现但不在第二个链表中出现的元素。下面是Python语言的源代码实现: ```python class Node: def __init__(self, value): self.value = value self.next = None def getDifferenceList(head1, head2): # 用两个集合分别存储链表1和链表2的元素 set1 = set() set2 = set() # 遍历链表1,将元素添加到集合set1中 p1 = head1 while p1: set1.add(p1.value) p1 = p1.next # 遍历链表2,将元素添加到集合set2中 p2 = head2 while p2: set2.add(p2.value) p2 = p2.next # 用集合操作求两个集合的差集 differenceSet = set1.difference(set2) # 将差集转换成链表返回 differenceList = None for value in differenceSet: newNode = Node(value) if not differenceList: differenceList = newNode else: p = differenceList while p.next: p = p.next p.next = newNode return differenceList ``` 以上代码的思路是先遍历链表,将链表中的元素分别添加到两个集合中,然后利用集合操作求两个集合的差集,最后将差集转换为链表返回。使用示例代码如下: ```python # 创建链表1:1->2->3->4->5 head1 = Node(1) head1.next = Node(2) head1.next.next = Node(3) head1.next.next.next = Node(4) head1.next.next.next.next = Node(5) # 创建链表2:3->4->6->7 head2 = Node(3) head2.next = Node(4) head2.next.next = Node(6) head2.next.next.next = Node(7) # 求差集 differenceList = getDifferenceList(head1, head2) # 输出差集链表元素:1 2 5 p = differenceList while p: print(p.value, end=' ') p = p.next ``` 输出结果为:1 2 5,表示在链表1中出现但不在链表2中出现的元素为1、2和5。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值