2.4 You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list. EXAMPLE: Input: (3 -> 1 -> 5) + (5 -> 9 -> 2); Output: 8 -> 0 -> 8
AddTwoLists1()类似于归并排序中的合并过程,但是需要加一个额外的if (carry == 1)判断,防止最高位进位丢失。
AddTwoLists2()是递归的方法,看了答案才知道。完全没有往递归上想。。。
AddTwoLists3()看了网友的程序后对AddTwoLists1()进行了一些改进。
package Question2_4;
public class Question2_4
{
public static void main(String[] args)
{
Question2_4 q1 = new Question2_4();
Question2_4 q2 = new Question2_4();
Question2_4 q3 = new Question2_4();
q1.AddToList(1);q1.AddToList(3);q1.AddToList(5);q1.AddToList(8); // 8531
q2.AddToList(2);q2.AddToList(9);q2.AddToList(2);q2.AddToList(3);q2.AddToList(9);
q2.AddToList(9);q2.AddToList(9);q2.AddToList(9);q2.AddToList(9);q2.AddToList(9); // 9999993292
q1.PrintList();
q2.PrintList();
q3 = AddTwoLists1(q1, q2);
q3.PrintList();
q3 = AddTwoLists2(q1, q2);
q3.PrintList();
q3 = AddTwoLists3(q1, q2);
q3.PrintList();
}
private Node head;
public Question2_4()
{
head = new Node(null, null);
}
public void AddToList(Integer e)
{
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = new Node(e, null);
}
public static Question2_4 AddTwoLists1(Question2_4 list1, Question2_4 list2)
{
Question2_4 res = new Question2_4();
Node pointer1 = list1.head.next;
Node pointer2 = list2.head.next;
Integer carry = 0;
while (pointer1 != null && pointer2 != null)
{
if (pointer1.data + pointer2.data + carry >= 10)
{
res.AddToList((pointer1.data + pointer2.data + carry)%10);
carry = 1;
}
else
{
res.AddToList(pointer1.data + pointer2.data + carry);
carry = 0;
}
pointer1 = pointer1.next;
pointer2 = pointer2.next;
}
// check the remainders of list1 or list2
while (pointer1 != null)
{
if (pointer1.data + carry >= 10)
{
res.AddToList((pointer1.data + carry)%10);
carry = 1;
}
else
{
res.AddToList(pointer1.data + carry);
carry = 0;
}
pointer1 = pointer1.next;
}
while (pointer2 != null)
{
if (pointer2.data + carry >= 10)
{
res.AddToList((pointer2.data + carry)%10);
carry = 1;
}
else
{
res.AddToList(pointer2.data + carry);
carry = 0;
}
pointer2 = pointer2.next;
}
if (carry == 1)
res.AddToList(1);
return res;
}
public static Question2_4 AddTwoLists2(Question2_4 list1, Question2_4 list2)
{
Question2_4 res = new Question2_4();
AddTwoLists2Helper(res, list1.head.next, list2.head.next, 0);
return res;
}
public static void AddTwoLists2Helper(Question2_4 res, Node node1, Node node2, int carry)
{
// base case of recursion
if (node1 == null && node2 == null)
{
if (carry == 0)
{
return;
}
else
{
res.AddToList(1);
return;
}
}
if (node1 == null)
{
res.AddToList((node2.data + carry)%10);
AddTwoLists2Helper(res, node1, node2.next, (node2.data + carry)/10);
}
else if (node2 == null)
{
res.AddToList((node1.data + carry)%10);
AddTwoLists2Helper(res, node1.next, node2, (node1.data + carry)/10);
}
else
{
res.AddToList((node1.data + node2.data + carry)%10);
AddTwoLists2Helper(res, node1.next, node2.next, (node1.data + node2.data + carry)/10);
}
}
public static Question2_4 AddTwoLists3(Question2_4 list1, Question2_4 list2)
{
Question2_4 res = new Question2_4();
Node pointer1 = list1.head.next;
Node pointer2 = list2.head.next;
Integer value = 0;
Integer carry = 0;
while (pointer1 != null || pointer2 != null)
{
if (pointer1 == null)
{
value = pointer2.data;
pointer2 = pointer2.next;
}
else if (pointer2 == null)
{
value = pointer1.data;
pointer1 = pointer1.next;
}
else
{
value = pointer1.data + pointer2.data;
pointer1 = pointer1.next;
pointer2 = pointer2.next;
}
value += carry;
res.AddToList(value%10);
carry = value/10;
}
if (carry == 1)
res.AddToList(1);
return res;
}
public void PrintList()
{
Node temp = head;
while (temp.next != null)
{
temp = temp.next;
System.out.print(temp.data + " ");
}
System.out.println();
}
private static class Node
{
Node(Integer d, Node n)
{
data = d;
next = n;
}
Integer data;
Node next;
}
}