`

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 ...

    cpp-算法精粹

    Number of 1 Bits Gray Code Single Number Single Number II Single Number III Power of Two Missing Number Maximum Product of Word Lengths Bitwise AND of Numbers Range Power of Three Rectangle Area 数论 ...

    算法导论--Introduction.to.Algorithms

    10.2 Linked lists 236 10.3 Implementing pointers and objects 241 10.4 Representing rooted trees 246 11 Hash Tables 253 11.1 Direct-address tables 254 11.2 Hash tables 256 11.3 Hash functions 262 11.4 ...

    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,剑指提供刷题笔记(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

Global site tag (gtag.js) - Google Analytics