两个链表相加

引言

我们继续加深/和%的操作,再来做一下这一道题.

题目

两个链表相加比如
9->3->7->null这是第一个链表,6->3->null这个是第二个链表,这两个相加为1->0->0->0->null.

解法1

我们通过两个栈实现

Stack<Integer> stack1=new Stack<Integer>();
        Stack<Integer> stack2=new Stack<Integer>();
        while(head1!=null)
        {
            stack1.push(head1.value);
            head1=head1.next;
        }
        while(head2!=null)
        {
            stack2.push(head2.value);
            head2=head2.next;
        }

这个时候,同时出栈,然后进行运算.下面就是操作的核心代码

int n1=0;
        int n2=0;
        int n=0;
        int ca=0;
        Node pre=null;
        Node node=null;
        while(!stack1.isEmpty()||!stack2.isEmpty())
        {
            n1=stack1.isEmpty()?0:stack1.pop();
            n2=stack2.isEmpty()?0:stack2.pop();
            n=n1+n2+ca;
            pre=node;
            node=new Node(n%10);
            node.next=pre;
            ca=n/10;
        }
        if(ca==1)
        {
            pre=node;
            node=new Node(1);
            node.next=pre;
        }
        return node;

记住,这个链表式从后向前建立的,同时我们需要注意pre和node的交替使用

pre=node;
node = new node;
node.next=pre;

然后循环往复,这是第一种解法,以下是第二种解法.
首先是逆序字符串

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

然后就是核心代码

public static Node addTwoList(Node head1,Node head2)
    {
        head1=reverseList(head1);
        head2=reverseList(head2);
        Node cur1=head1;
        Node cur2=head2;
        int n1;
        int n2;
        int n;
        int res;
        int ca=0;
        Node pre=null;
        Node node=null;
        while(cur1!=null||cur2!=null)
        {
            n1=cur1==null?0:cur1.value;
            n2=cur2==null?0:cur2.value;
            n=n1+n2+ca;
            pre=node;
            node=new Node(n%10);
            node.next=pre;
            ca=n/10;
            cur1 = cur1 != null ? cur1.next : null;
            cur2 = cur2 != null ? cur2.next : null;
        }
        if(ca==1)
        {
            pre=node;
            node=new Node(1);
            node.next=pre;
        }
        head1=reverseList(head1);
        head2=reverseList(head2);
        return node;
    }

总结

其中最重要的就是边计算,边生成链表.如何用最短的代码实现,我们拿第二个没有使用其他空间的方式在写一下(空间复杂度为O(1))

while(cur1!=null||cur2!=null)
{
         n1=cur1==null?0:cur1.value;
            n2=cur2==null?0:cur2.value;
            n=n1+n2+ca;
            pre=node;
            node=new Node(n%10);
            node.next=pre;
            ca=n/10;
            cur1 = cur1 != null ? cur1.next : null;
            cur2 = cur2 != null ? cur2.next : null;

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 题目 给定两个非空链表,每一个节点代表一个数字0-9,单个链表从左往右是由高位到低位组成的一个数,现在需要将两个链...
    zhouwaiqiang阅读 3,366评论 0 0
  • You are given two non-empty linked lists representing two...
    BlackChen阅读 113评论 0 1
  • 搞懂单链表常见面试题 Hello 继上次的 搞懂基本排序算法,这个一星期,我总结了,我所学习和思考的单链表基础知识...
    醒着的码者阅读 4,635评论 1 45
  • 吉尔伽美什用了什么方法这么短的时间打造了这么一个建筑,没有人知道。 恩奇都为过他,而吉尔伽美什冲他眨了眨眼说是秘密...
    茉莉与花剑阅读 896评论 1 2
  • 今天没有画画是看书,我看的是Billy Shiwell的极致水彩花卉教程。其中有一些话写的挺好的,在此分享: 原文...
    紫晶Jane阅读 257评论 0 1