两个单链表生成相加链表问题

johnyin2015 johnyin2015 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
问题见代码,这是一段大神写的代码。但有两行我没看懂,有知道的童鞋帮忙解答下,先谢谢了!
public  Node addList2(Node head1,Node head2)  
{
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
while(head1 != null){
stack1.push(head1.data);
head1 = head1.next;
}
while(head2 != null){
stack2.push(head2.data);
head2 = head2.next;
}
int n1=0; //链表1的数值
int n2=0; //链表2的数值
int n=0; //n1+n2+ca
int ca=0; //进位

Node node=null; //当前节点
Node pnode=null; //当前节点的前驱节点
while(!stack1.isEmpty()||!stack2.isEmpty())
{
n1 = stack1.isEmpty() ? 0 : stack1.pop();
n2 = stack2.isEmpty() ? 0 : stack2.pop();
n=n1+n2+ca;
node=new Node(n%10);
node.next=pnode;//根据下一行代码,node节点next域指向它自身,为什么要指向自身
pnode=node;//
ca=n/10;
}

if(ca == 1)
{
pnode = node;
node=new Node(n/10);
node.next=pnode;//问题同上
}

return node;
}
...全文
评论 2
收藏 已收藏
分享
返回顶部
johnyin2015
Java SE 62,621 成员 307,251 内容