力扣-两数相加

博客涉及Java和LeetCode相关内容,但具体信息缺失。推测可能是用Java语言解决LeetCode算法题等信息技术相关内容。
package com.JackChen.leetCode.Day1;

import java.util.Scanner;

/**
 * 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
 * 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
 * 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
 * 示例:
 * 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
 * 输出:7 -> 0 -> 8
 * 原因:342 + 465 = 807
 * 
 * @author Jack Chen
 *
 */
public class AddTwoNumber {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		while (scanner.hasNextLine()) {//键盘获取2个字符串以空格隔开
			String firstInput = scanner.nextLine();
			String secondInput = scanner.nextLine();
			//构建链表
			ListNode node1 = creatListNode(firstInput);
			ListNode node2 = creatListNode(secondInput);

            //打印链表
			print(addTwoNumbers(node1, node2));
		}
	}
	
	private static ListNode addTwoNumbers(ListNode node1, ListNode node2) {
		// TODO Auto-generated method stub
		
		ListNode node = new ListNode();//创建一个链表
		ListNode cur = node;//使用操作cur,因为操作完链表后,没有办法再次找到链表的头部
		
		int temp = 0;//进位
		while(node1 != null || node2 != null) {//只要有一个链表不为空,就需要计算
			int val1 = node1 == null ? 0 : node1.val;
			int val2 = node2 == null ? 0 : node2.val;
			
			int sum = val1 + val2 + temp;
			temp = sum/10;

			cur.next = new ListNode(sum%10);
			cur = cur.next;
			
			node1 = node1 == null ? null : node1.next;
			node2 = node2 == null ? null : node2.next;
		}

		if (temp != 0) {
			cur.next = new ListNode(temp);
		}
		return node.next;
	}

	private static ListNode creatListNode(String input) {
		// TODO Auto-generated method stub
		ListNode temp = new ListNode();
		ListNode node = temp;
		String[] arrays = input.split(" ");
		for (int i = 0; i < arrays.length; i++) {
			temp.next = new ListNode(Integer.parseInt(arrays[i]));
			temp = temp.next;
		}
		
		return node.next;
		
	}

	public static void print(ListNode node) {
		while(node != null) {
			System.out.print(node.val);
			node = node.next;
		}
	}
	public static class ListNode{
		int val;
		ListNode next;
		ListNode() {}
		ListNode(int val){
			this.val = val;
		}
		ListNode(int val, ListNode next){
			this.val = val;
			this.next = next;
		}
	}

}

力扣两数相加问题的描述为:给两个非空的链表,表示两个非负的整数,它们每位数字按照逆序的方式存储,且每个节点只能存储一位数字,需将两个数相加,并以相同形式返回一个表示和的链表,除数字 0 之外,这两个数都不会以 0 开头[^3]。 ### 解题思路 一种思路是调用函数`addTwoNumbers(l1, l2)`,传入两个链表,分别计算每个链表对应的数值(如`2->3->1`对应数值是 342),之后求出两个数值的和`sum`,最后通过`while`循环对`sum`取余和除 10 取整的操作再把各个位的数添加到一个链表中,最后返回链表[^2]。 ### 代码实现(Python 示例) ```python # 定义链表节点类 class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def addTwoNumbers(l1, l2): dummy = ListNode(0) current = dummy carry = 0 while l1 or l2 or carry: x = l1.val if l1 else 0 y = l2.val if l2 else 0 # 计算当前位的和以及进位 total = x + y + carry carry = total // 10 digit = total % 10 # 创建新节点并连接到结果链表 current.next = ListNode(digit) current = current.next # 移动到下一个节点 if l1: l1 = l1.next if l2: l2 = l2.next return dummy.next # 辅助函数:将列表转换为链表 def list_to_linked_list(lst): dummy = ListNode(0) current = dummy for val in lst: current.next = ListNode(val) current = current.next return dummy.next # 辅助函数:将链表转换为列表 def linked_list_to_list(head): result = [] current = head while current: result.append(current.val) current = current.next return result # 测试示例 l1 = list_to_linked_list([2, 4, 3]) l2 = list_to_linked_list([5, 6, 4]) result = addTwoNumbers(l1, l2) print(linked_list_to_list(result)) # 输出: [7, 0, 8] ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值