You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
题目大意:输入两个链表,求和
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode cur1= l1;
ListNode cur2= l2;
ListNode head = new ListNode(0);
ListNode cur = head;
int sum = 0;
while (cur1!= null || cur2 != null ){
if(cur1!= null){
int val1 = cur1.val;
sum = sum +val1;
cur1 = cur1.next;
}
if(cur2!= null) {
int val2 = cur2.val;
sum = sum + val2;
cur2 = cur2.next;
}
cur.next = new ListNode( sum % 10);
cur = cur.next;
sum = sum/10;
}
if(sum == 1){
cur.next = new ListNode(1);
}
return head.next;
}
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Follow up: What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input:(7->2->4->3)+(5->6->4)
Output:7->8->0->7
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<Integer> stack1 = addTwoNumbers_stack(l1);
Stack<Integer> stack2 = addTwoNumbers_stack(l2);
Stack<Integer> stack3 = new Stack<>();
ListNode dunny = new ListNode(0);
ListNode cur = new ListNode(0);
cur = dunny;
int num1=0; int num2=0;int sum=0;
int carry = 0 ;
while (!stack1.isEmpty()||!stack2.isEmpty()){
if (!stack1.isEmpty()){
num1 = stack1.pop();
}else num1=0;
if (!stack2.isEmpty()){
num2 = stack2.pop();
}else num2 = 0;
sum = (num1+num2+carry)%10;
carry = (num1+num2+carry)/10;
stack3.push(sum);
}
if (carry!=0) stack3.push(carry);
while (!stack3.isEmpty()){
cur.next = new ListNode(stack3.pop());
cur = cur.next;
}
return dunny.next;
}
public Stack<Integer> addTwoNumbers_stack(ListNode head){
if (head == null ) return null;
Stack<Integer> stack = new Stack<>();
ListNode dunny = new ListNode(0);
dunny.next = head;
while (dunny.next!=null){
stack.add(dunny.next.val);
dunny = dunny.next;
}
return stack;
}
反转链表,虽然题目不允许反转链表,但是这种做法还是有很大优越性的,空间复杂度减小为O(1);
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
l1 = reverseList(l1);
l2 = reverseList(l2);
ListNode res = addTwoNumbersii(l1,l2);
l1 = reverseList(l1);
l2 = reverseList(l2);
return reverseList(res);
}
public ListNode reverseList(ListNode head) {
if (head == null || head.next ==null) return head;
ListNode pre = null;
ListNode cur = head;
ListNode next;
while (cur !=null){
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
public ListNode addTwoNumbersii(ListNode l1, ListNode l2) {
ListNode cur1= l1;
ListNode cur2= l2;
ListNode head = new ListNode(0);
ListNode cur = head;
int sum = 0;
while (cur1!= null || cur2 != null ){ // 考虑到可能存在一个链表已经到达尾部
if(cur1!= null){
int val1 = cur1.val;
sum = sum +val1;
cur1 = cur1.next;
}
if(cur2!= null) {
int val2 = cur2.val;
sum = sum + val2;
cur2 = cur2.next;
}
cur.next = new ListNode( sum % 10);
cur = cur.next;
sum = sum/10;
}
if(sum == 1){ //注意最高一位可能存在进位
cur.next = new ListNode(1);
}
return head.next;
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。