LeetCoe No.3 20181109_两链表数值相加

原题:
给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

解题:
自定义了一个简单链表结构:

 public class LinkList {
    public LinkNode head;
    public LinkList(){
        head = new LinkNode(null);
    }
    public void addNode(LinkNode addNode){
         head.addNode(addNode);
    }
    public void print(){
        LinkNode node = head.next;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("HEAD-->");
        while (node!= null) {
            stringBuilder.append(node.data+"-->");
            node = node.next;
        }
        System.out.println(stringBuilder.toString());
    }
}
class LinkNode{
    Integer data;
    LinkNode next;
    public LinkNode(Integer data){
        this.data = data;
    }
    protected void addNode(LinkNode node){
       if(this.next == null){
        this.next = node;
    }else{
        this.next.addNode(node);
    }
    }
    protected void insertNode(LinkNode node){
        if(this.next == null){
            this.next = node;
        } else {
           LinkNode temp = this.next ;
           this.next =  node;
           node.next =temp;
        }
    }
}

根据链表解题方法如下:

  private static LinkList sumLinkTwo(LinkList linkOne,LinkList linkTwo){

        LinkList result = new LinkList();
        LinkNode one = linkOne.head.next;
        LinkNode two = linkTwo.head.next;
        boolean isSuperBit = false;
        while (one != null && two != null) {
            int sumBit = one.data + two.data;
            sumBit += isSuperBit ? 1 : 0;
            if(sumBit >= 10){
                sumBit = sumBit % 10;
                isSuperBit = true;
            } else {
                isSuperBit = false;
            }
            result.addNode(new LinkNode(sumBit));
            one = one.next;
            two = two.next;
        }

        while (one != null){
            int sumBit = one.data;
            sumBit += isSuperBit ? 1 : 0;
            if(sumBit >= 10){
                sumBit = sumBit % 10;
                isSuperBit = true;
            } else {
                isSuperBit = false;
            }
            result.addNode(new LinkNode(sumBit));
            one = one.next;
        }

        while (two != null){
            int sumBit = two.data;
            sumBit += isSuperBit ? 1 : 0;
            if(sumBit >= 10){
                sumBit = sumBit % 10;
                isSuperBit = true;
            } else {
                isSuperBit = false;
            }
            result.addNode(new LinkNode(sumBit));
            two = two.next;
        }
        //遍历完成两个链表之后  要判断是否有溢出位
        if(isSuperBit ){
            result.addNode(new LinkNode(1));
        }
        return result;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值