`

Sum of Two Linked Lists - 1

 
阅读更多

Given two numbers represented by two lists, write a function that returns sum list. The sum list is list representation of addition of two input numbers.

Example 1

Input:
  First List: 5->6->3  // represents number 365
  Second List: 8->4->2 //  represents number 248
Output
  Resultant list: 3->1->6  // represents number 613

Example 2

Input:
  First List: 7->5->9->4->6  // represents number 64957
  Second List: 8->4 //  represents number 48
Output
  Resultant list: 5->0->0->5->6  // represents number 65005

Solution
Traverse both lists. One by one pick nodes of both lists and add the values. If sum is more than 10 then make carry as 1 and reduce sum. If one list has more elements than the other then consider remaining values of this list as 0. Following is Java implementation of this approach.

public ListNode addLinkedList(ListNode a, ListNode b) {
	ListNode result = new ListNode(0);
	ListNode dummy = result;
	int carry = 0;
	while(a!=null || b!=null) {
		int sum = carry + ((a!=null)?a.val:0) + ((b!=null)?b.val:0);
		carry = sum / 10;
		result.next = new ListNode(sum%10);
		result = result.next;
		if(a!=null) a=a.next;
		if(b!=null) b=b.next;
	}
	if(carry>0) {
		result.next = new ListNode(carry);
	}
	return dummy.next;
}

 

分享到:
评论

相关推荐

    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 the two numbers and return ...

    leetcode2sumc-add-two-numbers-solution:我的LeetCodeC解决方案:添加两个数字

    lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may ...

    LeetCode最全代码

    191 |[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C++](./C++/number-of-1-bits.cpp) [Python](./Python/number-of-1-bits.py) | _O(1)_ | _O(1)_ | Easy ||| 201 | [Bitwise AND of ...

    leetcode2sumc-LeetCode:LeetCode的一些题目

    sum c LeetCode 帮助文档 帮助文档存放在Help文件夹下。 文件名 文件描述 链接 complexitypython.txt Python的一些常规操作的复杂度统计 题目清单 Array(数组) ID Difficulty Title Java Python 1 Easy 两数之和 26 ...

    leetcode2sumc--Offer:-提供

    1 Easy Two Sum no 11 Medium Container With Most Water Linked List(链表) ID Difficulty Title Python C++ Blog 21 Easy Merge Two Sorted Lists 83 * Remove Duplicates from Sorted List 141 * Linked List ...

    leetcode2sumc-ack-CherishLeetCode:ack-CherishLeetCode

    1 Easy Two Sum no 11 Medium Container With Most Water Linked List(链表) ID Difficulty Title Python C++ Blog 21 Easy Merge Two Sorted Lists 83 * Remove Duplicates from Sorted List 141 * Linked List ...

    微软内部资料-SQL性能优化2

    A soft page fault is resolved from one of the modified, standby, free or zero page transition lists. Paging is represented by a number of counters including page faults/sec, page input/sec and page ...

    Leetcode题目+解析+思路+答案.pdf

    - **Number of 1 Bits**:计算一个整数中1的个数。 3. **树(Tree)**: - **Depth of Binary Tree**:计算二叉树的深度。 - **Construct Binary Tree**:根据给定的数组构建二叉树。 - **Binary Tree Level ...

    LeetCode刷题题解答案(c++).pdf 彻底搞懂了编程算法题,成功拿到了大厂offer!

    1. **合并两个有序链表(Merge Two Sorted Lists)** - **问题描述**:将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 - **解决方案**: - 使用递归或迭代的...

    LeetCodeAgri.zip

    2. **链表**:链表数据结构在LeetCode中也占据重要地位,如"删除链表中的元素"(Remove Element)和"两链表相交"(Intersection of Two Linked Lists)。Swift虽然没有内置链表,但开发者可以自定义节点结构体来模拟...

    leetcode常见的100热点算法题

    6. **链表操作**:链表题目考察了指针操作和逻辑思维,如"Reverse Linked List"(反转链表)和"Merge Two Sorted Lists"(合并两个排序链表)。 7. **哈希表**:哈希表的高效查找和存储特性使得它在解决许多问题时...

    常见算法题答案及解析

    23. Merge K Sorted Linked Lists:合并K个排序链表。 24. Copy List with Random Pointer:复制含有随机指针的链表。 四、二叉树 25. Validate Binary Search Tree:验证二叉搜索树的合法性。 26. Maximum Depth ...

    常见算法题和解题思路.docx

    #### 一、两数之和(Two Sum) **问题描述:** 给定一个整数数组 `nums` 和一个整数目标值 `target`,请在该数组中找出和为目标值的那两个整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答案。但是,...

    LeetCode

    例如,“两链表的交点”(Intersection of Two Linked Lists)要求找出两个链表的第一个公共节点。 3. **字符串(Strings)**:处理字符串的拼接、查找、替换等问题。如“无重复字符的最长子串”(Longest ...

    :猴子:LeetCode,剑指提供刷题笔记(C / c++, Python3实现)

    LeetCode 原创文章每周最少两篇...Two Sum no 11 Medium Container With Most Water Linked List(链表) ID Difficulty Title Python C++ Blog 21 Easy Merge Two Sorted Lists 83 * Remove Duplicates from Sorted Lis

    LeetCode练习答案

    - **计算1的位数(Number of 1 Bits)**: 计算一个无符号整数的二进制表示中有多少个1位。 ##### 树(Tree) - **二叉树的深度(Depth of Binary Tree)**: 计算一棵二叉树的深度。 - **构建二叉树(Construct Binary ...

    leetcode:LeetCode练习

    3. **链表操作**:如“两链表的交点”(Intersection of Two Linked Lists),需要找到两个链表的第一个公共节点。 4. **树结构**:如“二叉树的最近公共祖先”(Lowest Common Ancestor of a Binary Tree),需要找到...

    leetCode:Leetcode解决方案

    例如,Reverse Linked List(反转链表)和Intersection of Two Linked Lists(两个链表的交集)等题目,都需要对链表进行操作。 3. 栈与队列:栈具有后进先出(LIFO)特性,队列则遵循先进先出(FIFO)原则。例如,...

Global site tag (gtag.js) - Google Analytics