python实现单链表,及两个链表按位相加

leetcode上一道链表题:
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7

# -*- coding: utf-8 -*-
"""
链表的实现
"""
class Node(object):
    '''
    data: 节点保存的数据
    _next: 保存下一个节点对象
    '''
    def __init__(self, val, next=None):
        self.val = val
        self.next = next
    def __repr__(self):
        '''
        用来定义Node的字符输出,
        print为输出data
        '''
        return str(self.val)
        
def deleteNode(self, node):# 不返回就是改变自身
    """
    :type node: ListNode
    :rtype: void Do not return anything, modify node in-place instead.
    """
    node.val = node.next.val
    node.next = node.next.next
def Creatlist(n):  
    if n<=0:  
        return False  
    if n==1:  
        return Node(1)    # 只有一个节点  
    else:  
        root=Node(1)  
        tmp=root  
        for i in range(2,n+1):       #  一个一个的增加节点  
            tmp.next=Node(i)  
            tmp=tmp.next  
    return root            # 返回根节点 

def printlist(head):       # 打印链表  
    p=head  
    while p!=None:  
        print p.val  
        p=p.next
        
def listlen(head):       # 链表长度  
    c=0  
    p=head  
    while p!=None:  
        c=c+1  
        p=p.next  
    return c 

def insert(head, n): #插入
    if n<1 or n>listlen(head):
        return
    
    p = head
    for i in range(1, n-1):
        p=p.next
    a = raw_input('val:')
    t = Node(val = a)
    t.next = p.next
    p.next = t #重点,改变插入的指针,再改变前一位的指针
    return head

def dellist(head, n): #删除
    if n<1 or n>listlen(head):
        return
    elif n is 1:  
        head=head.next   # 删除头 
    else:
        p = head
        for i in range(1, n-1):
            p=p.next
        p.next = p.next.next
    return head

l1 = Creatlist(4)
l1.val=7
l1.next.val=2
l1.next.next.val = 4
l1.next.next.next.val = 3
printlist(l1)
l2 = Creatlist(3)
l2.val=5
l2.next.val=6
l2.next.next.val = 4
printlist(l2)
p1 = l1
p2 = l2
arr1 = []
arr2 = []
while p1 != None:
    arr1.append(p1.val)
    p1 = p1.next
while p2 != None:
    arr2.append(p2.val)
    p2 = p2.next
ans = []
plus = 0
for i in range(max(len(arr1),len(arr2))):
    try:
        num = arr1[-i-1]+arr2[-i-1] + plus
        if num>=10:
            plus = 1
            n = num%10
        else:
            n = num
            plus = 0
        ans.insert(0,n)
    except:
        n = arr1[-i-1] + plus
        ans.insert(0,n)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值