Last active
June 6, 2018 15:51
-
-
Save sagiavinash/342c9930d7425194684532466d9c47ec to your computer and use it in GitHub Desktop.
NumberLinkedListAdder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sagiavinash.test; | |
import java.util.Scanner; | |
/* | |
Sample Standard Input: | |
1 1 1 | |
1 1 | |
Sample Standard Output: (addition) | |
1 2 2 | |
*/ | |
public class NumberLinkedListAdder { | |
public static void main(String args[]) { | |
Scanner scan = new Scanner(System.in); | |
NumberLinkedListAdder adder = new NumberLinkedListAdder(); | |
LinkedList<Integer> numList1 = adder.getNumberLinkedListFromLine(scan.nextLine()); | |
LinkedList<Integer> numList2 = adder.getNumberLinkedListFromLine(scan.nextLine()); | |
System.out.println("num1 string: " + adder.getLineFromNumberLinkedList(numList1)); | |
System.out.println("num2 string: " + adder.getLineFromNumberLinkedList(numList2)); | |
LinkedList<Integer> addedList = adder.addNumberLinkedLists(numList1, numList2); | |
System.out.println("added num string: " + adder.getLineFromNumberLinkedList(addedList)); | |
} | |
LinkedList<Integer> getNumberLinkedListFromLine(String str) { | |
String[] digits = str.split("\\s+"); | |
LinkedList<Integer> num = new LinkedList<Integer>(); | |
for (String x: digits) { | |
num.insertBegin(Integer.parseInt(x)); | |
} | |
return num; | |
} | |
String getLineFromNumberLinkedList(LinkedList<Integer> numList) { | |
int len = numList.getLength(); | |
String[] digits = new String[len]; | |
LLNode<Integer> digit = numList.head; | |
for (int i = 0; i < len; i++) { | |
digits[i] = Integer.toString(digit.getValue() != null ? digit.getValue() : 0); | |
digit = digit.getNext(); | |
} | |
return String.join(" ", digits); | |
} | |
LinkedList<Integer> addNumberLinkedLists(LinkedList<Integer> numList1, LinkedList<Integer> numList2) { | |
LinkedList<Integer> result = new LinkedList<Integer>(); | |
int lenDiff = (numList1.getLength() - numList1.getLength()); | |
LinkedList<Integer> smallerList = (lenDiff < 0) ? numList1 : numList2; | |
LinkedList<Integer> biggerList = (lenDiff < 0) ? numList2 : numList1; | |
int i, carry = 0; | |
LLNode<Integer> smallNumDigit = smallerList.head; | |
LLNode<Integer> bigNumDigit = biggerList.head; | |
for (i = 0; i < smallerList.getLength(); i++) { | |
int value = (carry + smallNumDigit.getValue() + bigNumDigit.getValue()) % 10; | |
carry = (carry + smallNumDigit.getValue() + bigNumDigit.getValue()) / 10; | |
smallNumDigit = smallNumDigit.getNext(); | |
bigNumDigit = bigNumDigit.getNext(); | |
result.insertBegin(value); | |
} | |
System.out.println("extra"); | |
for (;i < biggerList.getLength(); i++) { | |
int value = (carry + bigNumDigit.getValue()) % 10; | |
carry = (carry + bigNumDigit.getValue()) / 10; | |
bigNumDigit = bigNumDigit.getNext(); | |
result.insertBegin(value); | |
} | |
if (carry != 0) { | |
result.insertBegin(carry); | |
} | |
return result; | |
} | |
} | |
class LinkedList<T> { | |
public LLNode<T> head; | |
private LLNode<T> tail; | |
private int length = 0; | |
LinkedList() {} | |
LinkedList(T value) { | |
insertBegin(value); | |
} | |
int insertBegin(T value) { | |
LLNode<T> newNode = new LLNode<T>(value); | |
if (length == 0) { | |
head = newNode; | |
head.setNext(head); | |
tail = head; | |
return ++length; | |
} | |
newNode.setNext(head); | |
head = newNode; | |
return ++length; | |
} | |
int getLength() { | |
return length; | |
} | |
} | |
class LLNode<T> { | |
T value; | |
LLNode<T> next; | |
LLNode() {} | |
LLNode(T value) { | |
this.setValue(value); | |
} | |
LLNode(T value, LLNode<T> next) { | |
this.setValue(value); | |
this.setNext(next); | |
} | |
void setValue(T value) { | |
this.value = value; | |
} | |
void setNext(LLNode<T> next) { | |
this.next = next; | |
} | |
T getValue() { | |
return value; | |
} | |
LLNode<T> getNext() { | |
return next; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment