leetcode java链表两数相加 ListNode

**
两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位数字。
将两个数相加,并以相同形式返回一个表示和的链表。
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.**
新建链表,避免两个都为空建假结点,用指针指向链表的最后一个结点

package listnode;

//链表 ListNode
public class ListNode {
	int val;
	ListNode next;
	ListNode() {
	}
	ListNode(int val) {
		this.val = val;
	}
	ListNode(int val, ListNode next) {
		this.val = val;
		this.next = next;
	}
}
package listnode;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode  dummy= new ListNode(-1); //NodeList val=-1 next=null
        int carry = 0;
        ListNode curr = dummy; //curr=ListNode(id=58) val=-1 next=null
        while(l1!=null || l2!=null){
            int a1 = l1 == null?0:l1.val;
            int a2 = l2 == null?0:l2.val;
            int sum = a1+a2+carry;
            carry = sum /10;
            curr.next = new ListNode(sum%10);//curr=ListNode(id=58) val=-1 next=ListNode(id=23) val=4 next=null
            curr=curr.next;//当前节点值 =null val=sum%5 //next=ListNode(id=23) val=4 next=null
            if(l1!=null) l1=l1.next;
            if(l2!=null) l2 =l2.next;
        }
        if(carry==1) curr.next=new ListNode(1);
        return dummy.next;

    }
}

public class MainClass {
    public static int[] stringToIntegerArray(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if (input.length() == 0) {
          return new int[0];
        }    
        String[] parts = input.split(",");
        int[] output = new int[parts.length];
        for(int index = 0; index < parts.length; index++) {
            String part = parts[index].trim();
            output[index] = Integer.parseInt(part);
        }
        return output;
    }   
    public static ListNode stringToListNode(String input) {
        // Generate array from the input
        int[] nodeValues = stringToIntegerArray(input);   
        // Now convert that list into linked list
        ListNode dummyRoot = new ListNode(0);
        ListNode ptr = dummyRoot;
        for(int item : nodeValues) {
            ptr.next = new ListNode(item);
            ptr = ptr.next;
        }
        return dummyRoot.next;
    }    
    public static String listNodeToString(ListNode node) {
        if (node == null) {
            return "[]";
        }   
        String result = "";
        while (node != null) {
            result += Integer.toString(node.val) + ", ";
            node = node.next;
        }
        return "[" + result.substring(0, result.length() - 2) + "]";
    }  
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            ListNode l1 = stringToListNode(line);
            line = in.readLine();
            ListNode l2 = stringToListNode(line);           
            ListNode ret = new Solution().addTwoNumbers(l1, l2);            
            String out = listNodeToString(ret);            
            System.out.print(out);
        }
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值