Thursday, September 5, 2013

2.5 ---- sum at two list (reverse and forward order)

Q:  (书上的forward 的解法,明显更繁琐些)

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 Ts 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: (7-> 1 -> 6) + (5 -> 9 -> 2).That is, 617 + 295.
Output: 2 -> 1 -> 9.That is, 912.
FOLLOW UP
Suppose the digits are stored in forward order. Repeat the above problem.
EXAMPLE
Input: (6 -> 1 -> 7) + (2 -> 9 -> 5).That is, 617 + 295.
Output: 9 -> 1 -> 2.That is, 912

A:
Reverse case:

simply extract the data into an inter, and add them ,then transform them back to the list form. ( in CtCi, they add on the list, directly. which make us to manipulate the math and (进位)

 
public static LinkList reverseOrderSum(LinkList list1, LinkList list2) {
  LinkList newList = new LinkList();
  int sum1 = 0;
  int sum2 = 0;
  Node runner = null;
  int numDigits = 0;
  // calculate sum of list1,
  runner = list1.header.next;
  while (runner != null) {
   sum1 += runner.data * (Math.pow(10, numDigits));
   runner = runner.next;
   numDigits++;
  }
  // calculate sum of list2,
  runner = list2.header.next;
  numDigits = 0;
  while (runner != null) {
   sum1 += runner.data * (Math.pow(10, numDigits));
   runner = runner.next;
   numDigits++;
  }
  int total = sum1 + sum2;
  Integer totalInt = new Integer(total);
  String strSum = totalInt.toString();
  System.out.println(strSum);
  for (int i = strSum.length() - 1; i >= 0; i--) {
   String subChar = strSum.substring(i, i + 1);
   int nodeData = Integer.valueOf(subChar);
   newList.append(new Node(nodeData));
  }

  return newList;

 }

Forward case:
 public static LinkList forwardOrderSum(LinkList list1, LinkList list2) {
  LinkList newList = new LinkList();
  int sum1 = 0;
  int sum2 = 0;
  Node runner = null;
  // calculate sum of list1,
  runner = list1.header.next;
  while (runner != null) {
   sum1 = sum1 * 10 + runner.data;
   runner = runner.next;
  }
  // calculate sum of list2,
  runner = list2.header.next;
  while (runner != null) {
   sum2 = sum2 * 10 + runner.data;
   runner = runner.next;
  }

  int total = sum1 + sum2;
  Integer totalInt = new Integer(total);
  String strSum = totalInt.toString();
  System.out.println(strSum);

  for (int i = 0; i < strSum.length(); i++) {
   String subChar = strSum.substring(i, i + 1);
   int nodeData = Integer.valueOf(subChar);
   newList.append(new Node(nodeData));
  }
  return newList;

 }

Mistakes:
1:   因此, 不能简单得用 10^2 来期待得到100, 而应该用Math.pow(10,2)
^ is the bitwise exclusive OR (XOR) operator in Java (and many other languages). It is not used for exponentiation. For that, you must use Math.pow.

2 comments:

  1. I am almost 100% sure that interviewers don't expect to see this solution. They just need to ask: what if those numbers represented by linked list are too large to be stored in a 32bit or 64bit integer?

    ReplyDelete