利用链表计算两数之和

package com.lz;

public class Solution {
    //创建一个表示链表的类
    public static class ListNode{
        //节点的值
        public int val;
        //指向下一个链表节点
        public ListNode next;
        public ListNode(int val){
            this.val=val;
        }
        public ListNode(int val, ListNode next){
            this.val=val;
            this.next=next;
        }
    }

    /*两数之和
    输入两个非空的链表,表示两个非负的整数,其中它们的每个数字都是按照逆序存储的,并且每个节点存储一个数字
    将两个数相加,并以相同的形式返回一个表示和的链表
     */
    public ListNode addTwoNumbers(ListNode l1, ListNode l2){

        int sum,jinwei=0,num,val1=0,val2=0;
        //创建一个存储结果的根节点
        ListNode root=new ListNode(0);
        ListNode root0=root;
        //对两个链表同时进行遍历,获得两个加数
        while(l1!=null||l2!=null||jinwei!=0){
            if(l1!=null){
                val1=l1.val;
                //遍历l1链表
                l1=l1.next;
            }else{
                val1=0;
            }
            if(l2!=null){
                val2=l2.val;
                l2=l2.next;
            }else{
                val2=0;
            }


            sum=val1+val2+jinwei;
            //进位的值
            jinwei=sum/10;
            //当前结果
            num=sum%10;

            ListNode Lr=new ListNode(num);
            root.next=Lr;
            root=Lr;
        }
        return root0.next;
    }
}

  • 方法测试
package com.lz;

import java.util.Scanner;
import com.lz.Solution.ListNode;
public class Main {

    public static void main(String[] args) {
        Solution solution = new Solution();

        /*
        从键盘输入数字,逆序存入链表  String str = sc.nextLine().toString();
         */
        Scanner sc=new Scanner(System.in);
        while(true){
            String s1=sc.nextLine();
            String s2=sc.nextLine();


            int[] num1=new int[s1.length()];
            int[] num2=new int[s2.length()];

            for(int i=0;i<s1.length();i++){
            	//字符和整型数值之间相差48
                num1[i]=s1.charAt(i)-48;
            }
            for(int i=0;i<s2.length();i++){
            	//字符和整型数值之间相差48
                num2[i]=s2.charAt(i)-48;
            }


            ListNode l1 = new ListNode(num1[s1.length()-1]);
            ListNode root1=l1;
            for(int i=s1.length()-2;i>=0;i--){
                ListNode r=new ListNode(num1[i]);
                l1.next=r;
                l1=r;
            }
            ListNode l2 = new ListNode(num2[s2.length()-1]);
            ListNode root2=l2;
            for(int i=s2.length()-2;i>=0;i--){
                ListNode r=new ListNode(num2[i]);
                l2.next=r;
                l2=r;
            }

            ListNode result = solution.addTwoNumbers(root1, root2);

            //输出
            while(result!=null){
                ListNode r=new ListNode(0);

                int n=result.val;
                result=result.next;
                System.out.print(n);
                if(result==null){
                    System.out.print("\n");
                }

            }
        }




    }
}

  • 结果展示

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于链表的 C 语言代码,用于计算两个大数的乘积。 ```c #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; typedef struct node Node; Node *create_node(int num) { Node *new_node = (Node *) malloc(sizeof(Node)); new_node->data = num; new_node->next = NULL; return new_node; } Node *insert_at_end(Node *head, int num) { Node *new_node = create_node(num); if (head == NULL) { head = new_node; return head; } Node *temp = head; while (temp->next != NULL) { temp = temp->next; } temp->next = new_node; return head; } Node *reverse_list(Node *head) { Node *prev = NULL, *current = head, *next = NULL; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } return prev; } Node *multiply_lists(Node *head1, Node *head2) { Node *result = NULL; Node *temp1 = head1, *temp2; int carry = 0, sum; while (temp1 != NULL) { temp2 = head2; Node *new_node = NULL; carry = 0; while (temp2 != NULL) { sum = (temp1->data * temp2->data) + carry; carry = sum / 10; new_node = insert_at_end(new_node, sum % 10); temp2 = temp2->next; } if (carry > 0) { new_node = insert_at_end(new_node, carry); } new_node = reverse_list(new_node); for (int i = 0; i < temp1->data; i++) { new_node = insert_at_end(new_node, 0); } result = add_lists(result, new_node); temp1 = temp1->next; } return result; } Node *add_lists(Node *head1, Node *head2) { Node *result = NULL; Node *temp1 = head1, *temp2 = head2; int carry = 0, sum; while (temp1 != NULL || temp2 != NULL) { sum = carry + (temp1 != NULL ? temp1->data : 0) + (temp2 != NULL ? temp2->data : 0); carry = sum / 10; result = insert_at_end(result, sum % 10); if (temp1 != NULL) { temp1 = temp1->next; } if (temp2 != NULL) { temp2 = temp2->next; } } if (carry > 0) { result = insert_at_end(result, carry); } return result; } void display_list(Node *head) { Node *temp = head; while (temp != NULL) { printf("%d", temp->data); temp = temp->next; } printf("\n"); } int main() { Node *num1 = NULL, *num2 = NULL, *result = NULL; char c; int num; printf("Enter first number:\n"); while ((c = getchar()) != '\n') { num = c - '0'; num1 = insert_at_end(num1, num); } printf("Enter second number:\n"); while ((c = getchar()) != '\n') { num = c - '0'; num2 = insert_at_end(num2, num); } num1 = reverse_list(num1); num2 = reverse_list(num2); result = multiply_lists(num1, num2); printf("Result:\n"); display_list(result); return 0; } ``` 该代码将两个大数存储为链表,并使用一个辅助函数来反转链表。然后,它将两个链表相乘,产生一个新的链表,该链表包含结果。最后,它将结果链表打印到控制台上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值