📜  将单个数字乘以表示为链接列表的数字

📅  最后修改于: 2022-05-13 01:56:06.165000             🧑  作者: Mango

将单个数字乘以表示为链接列表的数字

给定一个链表 N个节点 其中每个节点代表一个数字的数字和一个数字M ,任务是将列表乘以M并打印结果链表。

例子:

方法:由于我们允许反转 LL 并最多添加 1 个额外的节点,因此解决此问题的想法是首先反转链表。然后,遍历链表并开始将M与每个节点相乘,并在每个乘法步骤之后添加生成的进位并更新进位。再次,反转修改后的链表并打印结果列表。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Node of a Linked List
class Node {
public:
    int data;
    Node* next;
};
 
// Function to create a new
// node with given data
Node* newNode(int data)
{
    // Initialize new node
    Node* new_node = new Node;
 
    // Set the data field of the node
    new_node->data = data;
    new_node->next = NULL;
 
    // Return the new node
    return new_node;
}
 
// Function to reverse the linked list
Node* reverse(Node* head)
{
    Node* prev = NULL;
    Node* current = head;
    Node* next;
 
    // Traverse until curr!=null
    while (current != NULL) {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
 
    // Return the head of the
    // reversed linked list
    return prev;
}
 
// Utility function to multiply a single digit
// to a linked list
Node* multiplyHelp(Node* head, int M)
{
    // Store the head of list
    Node* res = head;
 
    // Stores the address of previous node
    Node* prev = NULL;
 
    // Initially set carry as 0 and product as 1
    int carry = 0, product = 1;
 
    while (head != NULL) {
 
        // Multiply M with each digit
        product = head->data * M;
 
        // Add carry to product if carry exist
        product += carry;
 
        // Update carry for next calculation
        carry = product / 10;
 
        // Update the value of each nodes
        head->data = product % 10;
 
        // Move head and temp pointers to next nodes
        prev = head;
        head = head->next;
    }
 
    // If some carry is still there,
    // add a new node to list
    if (carry > 0)
        prev->next = newNode(carry);
 
    // Return head of the resultant list
    return res;
}
 
// Function to multiply a single digit
// to a linked list
Node* multiply(Node* head, int M)
{
    // Reverse linked list
    head = reverse(head);
 
    // Multiply M from left to right of reversed list
    head = multiplyHelp(head, M);
 
    // Reverse the modified list and return its head
    return reverse(head);
}
 
// Function to print the linked list
void printList(Node* node)
{
    while (node != NULL) {
        cout << node->data;
        node = node->next;
    }
}
 
// Driver Code
int main()
{
    // Given Input list: 1->2->7->3
    Node* head = newNode(1);
    head->next = newNode(2);
    head->next->next = newNode(7);
    head->next->next->next = newNode(3);
    int M = 3;
 
    // Function Call
    head = multiply(head, M);
 
    // Print resultant list
    printList(head);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
  // Node of a Linked List
  static class Node {
    int data;
    Node next;
  };
 
  // Function to create a new
  // node with given data
  static Node newNode(int data)
  {
     
    // Initialize new node
    Node new_node = new Node();
 
    // Set the data field of the node
    new_node.data = data;
    new_node.next = null;
 
    // Return the new node
    return new_node;
  }
 
  // Function to reverse the linked list
  static Node reverse(Node head) {
    Node prev = null;
    Node current = head;
    Node next;
 
    // Traverse until curr!=null
    while (current != null) {
      next = current.next;
      current.next = prev;
      prev = current;
      current = next;
    }
 
    // Return the head of the
    // reversed linked list
    return prev;
  }
 
  // Utility function to multiply a single digit
  // to a linked list
  static Node multiplyHelp(Node head, int M) {
    // Store the head of list
    Node res = head;
 
    // Stores the address of previous node
    Node prev = null;
 
    // Initially set carry as 0 and product as 1
    int carry = 0, product = 1;
 
    while (head != null) {
 
      // Multiply M with each digit
      product = head.data * M;
 
      // Add carry to product if carry exist
      product += carry;
 
      // Update carry for next calculation
      carry = product / 10;
 
      // Update the value of each nodes
      head.data = product % 10;
 
      // Move head and temp pointers to next nodes
      prev = head;
      head = head.next;
    }
 
    // If some carry is still there,
    // add a new node to list
    if (carry > 0)
      prev.next = newNode(carry);
 
    // Return head of the resultant list
    return res;
  }
 
  // Function to multiply a single digit
  // to a linked list
  static Node multiply(Node head, int M)
  {
     
    // Reverse linked list
    head = reverse(head);
 
    // Multiply M from left to right of reversed list
    head = multiplyHelp(head, M);
 
    // Reverse the modified list and return its head
    return reverse(head);
  }
 
  // Function to print the linked list
  static void printList(Node node) {
    while (node != null) {
      System.out.print(node.data);
      node = node.next;
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
     
    // Given Input list: 1.2.7.3
    Node head = newNode(1);
    head.next = newNode(2);
    head.next.next = newNode(7);
    head.next.next.next = newNode(3);
    int M = 3;
 
    // Function Call
    head = multiply(head, M);
 
    // Print resultant list
    printList(head);
 
  }
}
 
// This code contributed by gauravrajput1


Python3
# Python program for the above approach
 
# Node of a Linked List
class Node:
    def __init__(self, data):
        self.data = data;
        self.next = None;
         
# Function to create a new
# Node with given data
def newNode(data):
 
    # Initialize new Node
    new_Node = Node(data);
 
    # Return the new Node
    return new_Node;
 
# Function to reverse the linked list
def reverse(head):
    prev = None;
    current = head;
    next = None;
 
    # Traverse until curr!=None
    while (current != None):
        next = current.next;
        current.next = prev;
        prev = current;
        current = next;
     
 
    # Return the head of the
    # reversed linked list
    return prev;
 
 
# Utility function to multiply a single digit
# to a linked list
def multiplyHelp(head, M):
    # Store the head of list
    res = head;
 
    # Stores the address of previous Node
    prev = None;
 
    # Initially set carry as 0 and product as 1
    carry = 0;
    product = 1;
 
    while (head != None):
 
        # Multiply M with each digit
        product = head.data * M;
 
        # Add carry to product if carry exist
        product += carry;
 
        # Update carry for next calculation
        carry = product // 10;
 
        # Update the value of each Nodes
        head.data = product % 10;
 
        # Move head and temp pointers to next Nodes
        prev = head;
        head = head.next;
     
 
    # If some carry is still there,
    # add a new Node to list
    if (carry > 0):
        prev.next = newNode(carry);
 
    # Return head of the resultant list
    return res;
 
 
# Function to multiply a single digit
# to a linked list
def multiply(head, M):
 
    # Reverse linked list
    head = reverse(head);
 
    # Multiply M from left to right of reversed list
    head = multiplyHelp(head, M);
 
    # Reverse the modified list and return its head
    return reverse(head);
 
# Function to print linked list
def printList(node):
    while (node != None):
        print(node.data,end="");
        node = node.next;
     
# Driver Code
if __name__ == '__main__':
 
    # Given Input list: 1.2.7.3
    head = newNode(1);
    head.next = newNode(2);
    head.next.next = newNode(7);
    head.next.next.next = newNode(3);
    M = 3;
 
    # Function Call
    head = multiply(head, M);
 
    # Print resultant list
    printList(head);
 
# This code is contributed by gauravrajput1


C#
// C# program for the above approach
using System;
 
public class GFG {
 
  // Node of a Linked List
  class Node {
    public int data;
    public Node next;
  };
 
  // Function to create a new
  // node with given data
  static Node newNode(int data)
  {
     
    // Initialize new node
    Node new_node = new Node();
 
    // Set the data field of the node
    new_node.data = data;
    new_node.next = null;
 
    // Return the new node
    return new_node;
  }
 
  // Function to reverse the linked list
  static Node reverse(Node head) {
    Node prev = null;
    Node current = head;
    Node next;
 
    // Traverse until curr!=null
    while (current != null) {
      next = current.next;
      current.next = prev;
      prev = current;
      current = next;
    }
 
    // Return the head of the
    // reversed linked list
    return prev;
  }
 
  // Utility function to multiply a single digit
  // to a linked list
  static Node multiplyHelp(Node head, int M) {
    // Store the head of list
    Node res = head;
 
    // Stores the address of previous node
    Node prev = null;
 
    // Initially set carry as 0 and product as 1
    int carry = 0, product = 1;
 
    while (head != null) {
 
      // Multiply M with each digit
      product = head.data * M;
 
      // Add carry to product if carry exist
      product += carry;
 
      // Update carry for next calculation
      carry = product / 10;
 
      // Update the value of each nodes
      head.data = product % 10;
 
      // Move head and temp pointers to next nodes
      prev = head;
      head = head.next;
    }
 
    // If some carry is still there,
    // add a new node to list
    if (carry > 0)
      prev.next = newNode(carry);
 
    // Return head of the resultant list
    return res;
  }
 
  // Function to multiply a single digit
  // to a linked list
  static Node multiply(Node head, int M)
  {
     
    // Reverse linked list
    head = reverse(head);
 
    // Multiply M from left to right of reversed list
    head = multiplyHelp(head, M);
 
    // Reverse the modified list and return its head
    return reverse(head);
  }
 
  // Function to print the linked list
  static void printList(Node node) {
    while (node != null) {
      Console.Write(node.data);
      node = node.next;
    }
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
     
    // Given Input list: 1.2.7.3
    Node head = newNode(1);
    head.next = newNode(2);
    head.next.next = newNode(7);
    head.next.next.next = newNode(3);
    int M = 3;
 
    // Function Call
    head = multiply(head, M);
 
    // Print resultant list
    printList(head);
 
  }
}
 
// This code contributed by Princi Singh


Javascript


输出
3819

时间复杂度: O(N)
辅助空间: O(1)