【12】Adds two numbers represented by a linked list and returns the sum as a linked list
2013-04-15 23:14
423 查看
Question: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
package CareerCup;
public class AddLists
{
public AddLists(){}
public LinkedList add(LinkedList l1, LinkedList l2)
{
LinkedList res = new LinkedList();
Node header1 = l1.header;
Node header2 = l2.header;
int addmore = 0;
while(!(l1.header==null && l2.header==null))
{
int value = addmore;
if(l1.header!=null) value += l1.header.data;
if(l2.header!=null) value += l2.header.data;
addmore = value/10;
value %= 10;
res.add(value);
l1.deleteHead();
l2.deleteHead();
}
if(addmore!=0) res.add(addmore);
return res;
}
public static void main(String[] args)
{
int[] data1 = new int[]{3,1,5};
int[] data2 = new int[]{5,9,2};
LinkedList ll1 = new LinkedList();
LinkedList ll2 = new LinkedList();
for(int i=0;i<data1.length;i++)
ll1.add(data1[i]);
for(int i=0;i<data2.length;i++)
ll2.add(data2[i]);
System.out.println("The linkedlist1:");
ll1.print();
System.out.println("The linkedlist2:");
ll2.print();
AddLists ad = new AddLists();
LinkedList llsum = ad.add(ll1, ll2);
System.out.println("The linkedlist sum:");
llsum.print();
}
}
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
package CareerCup;
public class AddLists
{
public AddLists(){}
public LinkedList add(LinkedList l1, LinkedList l2)
{
LinkedList res = new LinkedList();
Node header1 = l1.header;
Node header2 = l2.header;
int addmore = 0;
while(!(l1.header==null && l2.header==null))
{
int value = addmore;
if(l1.header!=null) value += l1.header.data;
if(l2.header!=null) value += l2.header.data;
addmore = value/10;
value %= 10;
res.add(value);
l1.deleteHead();
l2.deleteHead();
}
if(addmore!=0) res.add(addmore);
return res;
}
public static void main(String[] args)
{
int[] data1 = new int[]{3,1,5};
int[] data2 = new int[]{5,9,2};
LinkedList ll1 = new LinkedList();
LinkedList ll2 = new LinkedList();
for(int i=0;i<data1.length;i++)
ll1.add(data1[i]);
for(int i=0;i<data2.length;i++)
ll2.add(data2[i]);
System.out.println("The linkedlist1:");
ll1.print();
System.out.println("The linkedlist2:");
ll2.print();
AddLists ad = new AddLists();
LinkedList llsum = ad.add(ll1, ll2);
System.out.println("The linkedlist sum:");
llsum.print();
}
}
相关文章推荐
- [CrackCode] 2.4 Add two numbers and return the sum as a linked list
- The two numbers and return it as a linked list
- Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers
- Add two numbers represented by linked lists
- Add two numbers represented by linked lists | Set 2
- 把两个链表加起来 add two numbers represented by two linked lists
- Represent an integer by the sum of at least two consecutive integers
- 周刷题第一期总结(two sum and two numbers)
- Display the linear linked list by traversing reverse
- The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
- Two Solutions to fix issue: ORDER BY items must appear in the select list if the statement contains a UNION, ...
- define a class for a linked list and write a method to delete the nth node.
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
- add two linked list as integer
- The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact
- (java) Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
- Ch2-5: find the beginning of loop in a circular linked list---two solutions
- add two linked list as integer
- Swap kth element from the beginning and kth element from the end of linked list -- Amazon
- Exchange the numbers of row and array of a two-dimensional array, and form a new two-dimensional ar