题解 | #链表相加(二)#
链表相加(二)
http://www.nowcoder.com/practice/c56f6c70fb3f4849bc56e33ff2a50b6b
\
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head1 ListNode类
* @param head2 ListNode类
* @return ListNode类
*/
public static ListNode reverse(ListNode node){
ListNode pre = null;
ListNode next = null;
while(node!=null){
next = node.next;
node.next=pre;
pre = node;
node = next;
}
return pre;
}
public ListNode addInList (ListNode head1, ListNode head2) {
// write code here
ListNode l1 = reverse(head1);
ListNode l2 = reverse(head2);
Stack<Integer> s = new Stack<>();
int j = 0;
int sum = 0;
while(l1!=null||l2!=null){
if(l1!=null){
sum=sum+l1.val;
l1=l1.next;
}
if(l2!=null){
sum=sum+l2.val;
l2=l2.next;
}
sum=sum+j;
int res = sum%10;
s.add(res);
if(sum>=10){
j=1;
}else{
j=0;
}
sum=0;
}
if(j==1){
s.add(1);
}
ListNode head;
ListNode node = new ListNode(s.pop());
head = node;
while(!s.isEmpty()){
ListNode nextnode = new ListNode(s.pop());
node.next=nextnode;
node = node.next;
}
return head;
}
}