常考数据结构与算法:两个链表生成相加链表

题目描述

假设链表中每一个节点的值都在 0 - 9 之间,那么链表整体就可以代表一个整数。

给定两个这种链表,请生成代表两个整数相加值的结果链表。

例如:链表 1 为 9->3->7,链表 2 为 6->3,最后生成新的结果链表为 1->0->0->0。

 

示例1

输入

[9,3,7],[6,3]

返回值

{1,0,0,0}

 

方案一:

1、将两个链表逆序,这样就可以依次得到从低到高位的数字

2、同步遍历两个逆序后链表,相加生成新链表,同时关注进位

3、当两个链表都遍历完成后,关注进位。

4、 将两个逆序的链表再逆序一遍,调整回去

package datastructure;

public class AddInListMe {
    /**
     *
     * @param head1 ListNode类
     * @param head2 ListNode类
     * @return ListNode类
     */
    public ListNode addInList (ListNode head1, ListNode head2) {
        head1 = reverseList(head1);
        head2 = reverseList(head2);

        int ca = 0;
        int n1 =0;
        int n2 =0;
        int n =0;

        ListNode c1 = head1;
        ListNode c2 = head2;
        ListNode node = null;
        ListNode pre = null;

        while(c1 !=null || c2!=null){
            n1 = c1 != null ? c1.val:0;
            n2 = c2 != null ? c2.val:0;
            n = n1+n2+ca;
            pre= node;
            node = new ListNode(n % 10); // 余数
            node.next=pre;
            ca=n/10;
            c1=c1 != null ? c1.next : null;
            c2=c2 != null ? c2.next : null;
        }

        if(ca == 1){
            pre=node;
            node = new ListNode(1);
            node.next = pre;
        }
        reverseList(head1);
        reverseList(head2);
        return node;
    }

    public  ListNode reverseList(ListNode head){
        ListNode pre = null;
        ListNode next = null;
        while(head!=null){
            next=head.next;
            head.next=pre;
            pre=head;
            head=next;
        }
        return pre;
    }
}

 

方案二:

   能把数据顺序反过来的数据结构,那不就是栈嘛,我们可以先将两个链表中的数存储在栈中,计算的时候依次出栈,再加上一个标志位 carry,我们就能做到计算个位的和,并向十位进位,再计算十位的和,并向百位进位,以此类推。。。

public ListNode addInList(ListNode head1, ListNode head2) {

        Stack<Integer> s1 = new Stack<>();
        Stack<Integer> s2 = new Stack<>();
        int ca = 0;

        while(null != head1){
            s1.push(head1.val);
            head1 = head1.next;
        }
        while(null != head2){
            s2.push(head2.val);
            head2 = head2.next;
        }

        int c1 = 0;
        int c2 = 0;
        int ret = 0;
        ListNode pre;
        ListNode node=null;
        while(!s1.isEmpty() || !s2.isEmpty()){
           c1 = 0;
           c2 = 0;
           if(!s1.isEmpty()){
               c1 = s1.pop();
           }

           if(!s2.isEmpty()){
               c2 = s2.pop();
           }

           ret = c1 + c2 + ca;
           pre = node;
           node = new ListNode(ret%10);
           node.next = pre;

           ca = ret/10;
        }

        if(1 == ca){
            pre = node;
            node = new ListNode(1);
            node.next = pre;
        }
        return node;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值