}if(carry == 1) cur.next =newListNode(1);returndummy.next; } } 此解法来自 http://www.cnblogs.com/grandyang/p/4129891.html Java解法2: /*** Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * }*/classS...
* 002-Add Two Numbers (单链表表示的两个数相加) *@paraml1 第一个数 *@paraml2 第二个数 *@return结果 */publicListNodeaddTwoNumbers(ListNode l1, ListNode l2){if(l1 ==null) {returnl2; }if(l2 ==null) {returnl1; }ListNodep1=l1;ListNodep2=l2;ListNoderoot=newListNode(0);// 头结点ListNo...
l2.add(new ListNode(4)); ListNode listNode = new AddTwoNumbers().addTwoNumbers(l1, l2); System.out.println(listNode.val); while (listNode.next != null) { System.out.println(listNode.next.val); listNode = listNode.next; } } } class ListNode { int val; ListNode next; ListNode(int x...
public class ListNodeMain { private static Scanner sc; public static void main(String[] args) { System.out.println("start ..."); ListNode l1 = new ListNode(0), tmpListNode1 = l1; ListNode l2 = new ListNode(0), tmpListNode2 = l2; sc = new Scanner(System.in); System.out.println("...
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { Stack<Integer> s1 = new Stack<Integer>(); Stack<Integer> s2 = new Stack<Integer>(); while(l1 != null) { s1.push(l1.val); l1 = l1.next; }; while(l2 != null) { ...
Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add th...[LeetCode] (medium) 2. Add Two Numbers https://leetcode.com/problems/add-two-numbers/ You...
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { //初始化为0的节点 ListNode init = new ListNode(0); //当前输出节点位置,初始化为initListNode ListNode current = init; //进位值 int carry = 0; while (l1 != null || l2 != null) { ...
Leetcode刷题笔记2:Add Two Numbers(C语言) 错误示范: 开始的时候,我想的比较简单,就输入的时候直接倒转得到数n1,同理得n2, 然后相加得n后,取余尾插法就逆转数字了,如下: /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ... ...
每日小刷 Add Two Numbers Runtime Memory 4ms 2.4m // Definition for singly-linked list. // #[derive(PartialEq, Eq, Clone, Debug)] // pub struct ListNode { // pub ...
class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* pHead = new ListNode(0); ListNode* p = pHead; int carry... 2. Add Two Numbers 题目: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in rev...