Advertisement
Guest User

Untitled

a guest
May 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. public static LinkedList<Integer> sum(LinkedList<Integer> op1, LinkedList<Integer> op2) {
  2.  
  3. // Lista rezultanta
  4. LinkedList<Integer> resultList = new LinkedList<Integer>();
  5. // Iteratorii listelor
  6. ListIterator<Integer> it1 = op1.listIterator(op1.size());
  7. ListIterator<Integer> it2 = op2.listIterator(op2.size());
  8.  
  9. Integer sum = 0;
  10. Integer coada = 0;
  11. while (it1.hasPrevious() && it2.hasPrevious()) {
  12. sum = it1.previous() + it2.previous() + coada;
  13. if (sum > 0) {
  14. resultList.add(sum % 10);
  15. coada = 1;
  16. } else {
  17. resultList.add(sum);
  18. coada = 0;
  19. }
  20. }
  21.  
  22. if (it1.hasPrevious() || it2.hasPrevious()) {
  23. Integer aux = 0;
  24. if (it1.hasPrevious() && it2.hasPrevious()) {
  25. aux = it1.previous() + it2.previous();
  26. } else {
  27. aux = it1.hasPrevious() ? it1.previous() : it2.previous();
  28. }
  29. sum = aux;
  30. if (sum > 0) {
  31. resultList.add(sum % 10);
  32. coada = 1;
  33. } else {
  34. resultList.add(sum);
  35. coada = 0;
  36. }
  37. }
  38.  
  39.  
  40. Collections.reverse(resultList);
  41.  
  42. return resultList;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement