Puede almacenar ambos operandos en la primera lista y luego almacenar su respuesta en la segunda lista. Multiplique como lo haría normalmente (si normalmente realiza la división como lo enseñan las escuelas en los EE. UU.), Agregue cada producto intermedio a la lista de respuestas a medida que avanza. No es el método más eficiente, pero funciona.
(Supongo que ambos operandos son enteros positivos ya que están almacenados en una lista vinculada. Si necesita tener en cuenta los números negativos, simplemente verifique el primer nodo de cada lista y determine si el resultado es negativo. Luego convierta cada lista en positiva número, multiplíquelo como se describe y agregue un negativo después si es necesario.)
Código Java (mayormente no probado)
- Cómo prepararse para entrevistas tecnológicas mientras tiene un trabajo
- Dada una entrada 'n' del usuario, debe encontrar todos los números de la suerte del 1 al n. Los números de la suerte son aquellos que tienen 4 o 7 o ambos como dígitos. ¿Cuál es el método más eficiente para resolver este problema?
- Quiero prepararme para mis rondas de entrevistas técnicas por JP Morgan (sucursal de Mumbai). ¿Qué preguntas hace JP Morgan para un desarrollador Java experimentado de 5 años?
- Soy un programador decente y me graduaré este mes con rechazos de MathWorks, Bloomberg, Amazon, etc. ¿Cómo debo abordar las entrevistas?
- ¿Por qué los montones de Fibonacci se llaman montones de Fibonacci?
import java.util.LinkedList; import java.util.ListIterator; public class MultiplyLinkedLists { public static void main(String[] args) { LinkedList operand1 = new LinkedList(); LinkedList operand2 = new LinkedList(); operand1.addLast(new Integer(9)); operand1.addLast(new Integer(9)); operand2.addLast(new Integer(2)); operand2.addLast(new Integer(2)); boolean success = multiply(operand1, operand2); if (success) { System.out.print("The result of the multiplication is: "); System.out.println(operand2); } else { System.out.println("The two numbers could not be multiplied."); } } public static boolean multiply(LinkedList operand1, LinkedList operand2) { // Edge case yields no change if (operand1 == null || operand2 == null) { return false; } int operand1Len = operand1.size(); int operand2Len = operand2.size(); int bothOperandsLen = operand1Len + operand2Len; // Move digits from operand 2 to the end of operand 1 while (!operand2.isEmpty()) { operand1.addLast(operand2.removeFirst()); } // Rename linked lists for clarity LinkedList bothOperands = operand1; LinkedList answer = operand2; // Initialize answer to 0 answer.add(0); ListIterator operand2Iter = bothOperands.listIterator(bothOperandsLen); // Iterate over second operand's digits in reverse order for (int i = 0; i < operand2Len; i++) { int operand2Digit = operand2Iter.previous().intValue(); ListIterator operand1Iter = bothOperands.listIterator(operand1Len); // Iterate over first operand's digits in reverse order for (int j = 0; j < operand1Len; j++) { int operand1Digit = operand1Iter.previous().intValue(); int product = operand1Digit * operand2Digit; int shiftAmount = i + j; // Add the intermediate product to the answer list add(answer, product, shiftAmount); } } return true; } private static void add(LinkedList result, int product, int shiftAmount) { int resultLen = result.size(); for (int insertPositionFromRight = shiftAmount; product != 0; insertPositionFromRight++) { int digit = product % 10; product /= 10; int numNodesNeeded = insertPositionFromRight - resultLen + 1; // Expand the width of the answer linked list if necessary for (int i = 0; i 10) { newVal -= 10; product++; } result.set(resultIndex, newVal); } } }