给出两个链表形式表示的数字,写一个函数得到这两个链表相乘乘积

package com.lxg.algorithm;

import java.util.Arrays;

public class Solution06 {
    /**
     * @param l1: the first list
     * @param l2: the second list
     * @return: the product list of l1 and l2
     */
    public static long multiplyLists(ListNode l1, ListNode l2) {
        // write your code here
        ListNode temp1 = l1;
        ListNode temp2 = l2;
        int len1 = 0,len2 = 0;
        while (temp1 != null) {
            len1++;
            temp1 = temp1.next;
        }
        StringBuilder str1 = new StringBuilder();
        temp1 = l1;
        for (int i = 0; i < len1; i++) {
            str1.append(String.valueOf(temp1.val));
            temp1 = temp1.next;
        }
        while (temp2!= null) {
            len2++;
            temp2 = temp2.next;
        }
        StringBuilder str2 = new StringBuilder();
        temp2 = l2;
        for (int i = 0; i < len2; i++) {
            str2.append(String.valueOf(temp2.val));
            temp2 = temp2.next;
        }
        System.out.println("str1 = " + str1);
        System.out.println("str2 = " + str2);
        long num1 = Integer.parseInt(String.valueOf(str1));
        long num2 = Integer.parseInt(String.valueOf(str2));
        return num1*num2;
    }

    public static void main(String[] args) {
        ListNode L1 = new ListNode(1);
        ListNode L2 = new ListNode(2);
        ListNode L3 = new ListNode(3);
        ListNode L4 = new ListNode(4);
        L1.next = L2;
        L3.next = L4;
        long l = multiplyLists(L1, L3);
        System.out.println("l = " + l);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你设计一个简单的C++链表类,它可以存储数字,并且重载*运算符以实现链表中两个数字相乘。 首先,我们需要定义一个节点类,用于表示链表中的每个节点: ```cpp class Node { public: int val; Node* next; Node(int val): val(val), next(NULL) {} }; ``` 然后,我们可以定义链表类,其中包含了一些常用的链表操作,例如插入节点、删除节点和遍历链表: ```cpp class LinkedList { private: Node* head; public: LinkedList(): head(NULL) {} ~LinkedList() { Node* cur = head; while (cur != NULL) { Node* next = cur->next; delete cur; cur = next; } } void insert(int val) { Node* node = new Node(val); if (head == NULL) { head = node; } else { Node* cur = head; while (cur->next != NULL) { cur = cur->next; } cur->next = node; } } void remove(int val) { if (head == NULL) { return; } if (head->val == val) { Node* tmp = head; head = head->next; delete tmp; return; } Node* cur = head; while (cur->next != NULL && cur->next->val != val) { cur = cur->next; } if (cur->next != NULL) { Node* tmp = cur->next; cur->next = tmp->next; delete tmp; } } void print() { Node* cur = head; while (cur != NULL) { cout << cur->val << " "; cur = cur->next; } cout << endl; } int size() { int cnt = 0; Node* cur = head; while (cur != NULL) { cnt++; cur = cur->next; } return cnt; } Node* getHead() { return head; } friend LinkedList operator*(const LinkedList& a, const LinkedList& b) { LinkedList res; Node* cur_a = a.head; while (cur_a != NULL) { Node* cur_b = b.head; while (cur_b != NULL) { res.insert(cur_a->val * cur_b->val); cur_b = cur_b->next; } cur_a = cur_a->next; } return res; } }; ``` 在链表类中,我们还重载了*运算符,用于实现两个链表数字相乘。该函数返回一个新的链表,其中每个节点的val值为两个链表中对应节点val值的乘积。 现在,我们可以使用该链表类来存储数字,并且进行相乘操作了。例如,以下代码将创建两个链表,分别存储数字1、2、3和4、5,并计算它们的乘积: ```cpp int main() { LinkedList a, b; a.insert(1); a.insert(2); a.insert(3); b.insert(4); b.insert(5); LinkedList c = a * b; c.print(); // 输结果为:4 5 8 10 12 15 return 0; } ``` 希望这个简单的链表类能够对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值