问题:Add Two Numbers
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
不过后面提交的过程中,发现完全不需要反转链表,主要是243+564->708,狗日的342+465->708是相同的,造成要反转。
解题代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//ListNode pre1 = l1,cur = l1, next = null,pre2 = l2;
//int i = 0,j = 0;
//艹,发现想多了,返回数据根本不需要反转
//l1翻转
// while(cur != null){
// next = cur;
// cur = cur.next;
// next.next = pre1;
// pre1 = next;
// i++;//记录l1位数
// }
// l1.next = null;
//
// //l2反转
// cur = l2;
// while(cur != null){
// next = cur;
// cur = cur.next;
// next.next = pre2;
// pre2 = next;
// j++;//记录l2位数
// }
// l2.next = null;
//System.out.println("i:" + i);
//System.out.println("j:" + j);
//如果l1大于l2位数,则l2头结点补0;反之亦然
// if(i > j){
// for(int k = 0; k < i-j; k++){
// ListNode ln = new ListNode(0);
// ln.next = pre2;
// pre2 = ln;
// }
// }
// else{
// for(int k = 0; k < j-i; k++){
// ListNode ln = new ListNode(0);
// ln.next = pre1;
// pre1 = ln;
// }
// }
//两数相加
int addNum = 0;
ListNode last = null;
ListNode head = null;
boolean isOver = false;//是否进位
while(l1 != null && l2 != null){
if(isOver){
addNum = l1.val + l2.val + 1;
isOver = false;
}
else{
addNum = l1.val + l2.val;
}
if(addNum > 9){
isOver = true;
addNum = addNum % 10;//取个位数
}
ListNode ln = new ListNode(addNum);
if(head == null){
last = head = ln;
}else{
last.next = ln;
last = ln;
}
l1 = l1.next;
l2 = l2.next;
}
//如果l1,l2位数不相同,则必有且只有1个还有数据
while(l1 != null){
//如果有进位
if(isOver){
l1.val += 1;
isOver = false;
if(l1.val > 9){
isOver = true;
l1.val = l1.val % 10;//取个位数
}
}
last.next = l1;
last = l1;
l1 = l1.next;
}
while(l2 != null){
//如果有进位
if(isOver){
l2.val += 1;
isOver = false;
if(l2.val > 9){
isOver = true;
l2.val = l2.val % 10;//取个位数
}
}
last.next = l2;
last = l2;
l2 = l2.next;
}
//如果还有进位未处理
if(isOver){
ListNode ln = new ListNode(1);
last.next = ln;
last = ln;
}
//如果位数不相等,则把剩余的相加最后
// while(head != null)
// {
// System.out.println(head.val);
// head = head.next;
// }
return head;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//ListNode pre1 = l1,cur = l1, next = null,pre2 = l2;
//int i = 0,j = 0;
//艹,发现想多了,返回数据根本不需要反转
//l1翻转
// while(cur != null){
// next = cur;
// cur = cur.next;
// next.next = pre1;
// pre1 = next;
// i++;//记录l1位数
// }
// l1.next = null;
//
// //l2反转
// cur = l2;
// while(cur != null){
// next = cur;
// cur = cur.next;
// next.next = pre2;
// pre2 = next;
// j++;//记录l2位数
// }
// l2.next = null;
//System.out.println("i:" + i);
//System.out.println("j:" + j);
//如果l1大于l2位数,则l2头结点补0;反之亦然
// if(i > j){
// for(int k = 0; k < i-j; k++){
// ListNode ln = new ListNode(0);
// ln.next = pre2;
// pre2 = ln;
// }
// }
// else{
// for(int k = 0; k < j-i; k++){
// ListNode ln = new ListNode(0);
// ln.next = pre1;
// pre1 = ln;
// }
// }
//两数相加
int addNum = 0;
ListNode last = null;
ListNode head = null;
boolean isOver = false;//是否进位
while(l1 != null && l2 != null){
if(isOver){
addNum = l1.val + l2.val + 1;
isOver = false;
}
else{
addNum = l1.val + l2.val;
}
if(addNum > 9){
isOver = true;
addNum = addNum % 10;//取个位数
}
ListNode ln = new ListNode(addNum);
if(head == null){
last = head = ln;
}else{
last.next = ln;
last = ln;
}
l1 = l1.next;
l2 = l2.next;
}
//如果l1,l2位数不相同,则必有且只有1个还有数据
while(l1 != null){
//如果有进位
if(isOver){
l1.val += 1;
isOver = false;
if(l1.val > 9){
isOver = true;
l1.val = l1.val % 10;//取个位数
}
}
last.next = l1;
last = l1;
l1 = l1.next;
}
while(l2 != null){
//如果有进位
if(isOver){
l2.val += 1;
isOver = false;
if(l2.val > 9){
isOver = true;
l2.val = l2.val % 10;//取个位数
}
}
last.next = l2;
last = l2;
l2 = l2.next;
}
//如果还有进位未处理
if(isOver){
ListNode ln = new ListNode(1);
last.next = ln;
last = ln;
}
//如果位数不相等,则把剩余的相加最后
// while(head != null)
// {
// System.out.println(head.val);
// head = head.next;
// }
return head;
}
}
上面的代码用了递归排序的思想,不过实现太罗嗦了,下面为简洁的实现方式:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//两数相加
int addNum = 0;
ListNode last = new ListNode(0);
ListNode head = last;
int isOver = 0;//是否进位
while(true){
int v1 = l1 == null ? 0 : l1.val;
int v2 = l2 == null ? 0 : l2.val;
addNum = v1 + v2 + isOver;
isOver = addNum / 10;
addNum = addNum % 10;
last.val = addNum;
if(l1 != null)
<span style="white-space:pre"> </span>l1 = l1.next;
if(l2 != null)
<span style="white-space:pre"> </span>l2 = l2.next;
if(l1 == null && l2 == null && isOver == 0){
<span style="white-space:pre"> </span>break;
}
else{
<span style="white-space:pre"> </span>last.next = new ListNode(0);
<span style="white-space:pre"> </span>last = last.next;
}
}
return head;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//两数相加
int addNum = 0;
ListNode last = new ListNode(0);
ListNode head = last;
int isOver = 0;//是否进位
while(true){
int v1 = l1 == null ? 0 : l1.val;
int v2 = l2 == null ? 0 : l2.val;
addNum = v1 + v2 + isOver;
isOver = addNum / 10;
addNum = addNum % 10;
last.val = addNum;
if(l1 != null)
<span style="white-space:pre"> </span>l1 = l1.next;
if(l2 != null)
<span style="white-space:pre"> </span>l2 = l2.next;
if(l1 == null && l2 == null && isOver == 0){
<span style="white-space:pre"> </span>break;
}
else{
<span style="white-space:pre"> </span>last.next = new ListNode(0);
<span style="white-space:pre"> </span>last = last.next;
}
}
return head;
}
}