Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static LinkedList<Integer> sum(LinkedList<Integer> op1, LinkedList<Integer> op2) {
- // Lista rezultanta
- LinkedList<Integer> resultList = new LinkedList<Integer>();
- // Iteratorii listelor
- ListIterator<Integer> it1 = op1.listIterator(op1.size());
- ListIterator<Integer> it2 = op2.listIterator(op2.size());
- Integer sum = 0;
- Integer coada = 0;
- while (it1.hasPrevious() && it2.hasPrevious()) {
- sum = it1.previous() + it2.previous() + coada;
- if (sum > 0) {
- resultList.add(sum % 10);
- coada = 1;
- } else {
- resultList.add(sum);
- coada = 0;
- }
- }
- if (it1.hasPrevious() || it2.hasPrevious()) {
- Integer aux = 0;
- if (it1.hasPrevious() && it2.hasPrevious()) {
- aux = it1.previous() + it2.previous();
- } else {
- aux = it1.hasPrevious() ? it1.previous() : it2.previous();
- }
- sum = aux;
- if (sum > 0) {
- resultList.add(sum % 10);
- coada = 1;
- } else {
- resultList.add(sum);
- coada = 0;
- }
- }
- Collections.reverse(resultList);
- return resultList;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement