Leetcode 线性表 数 Add Two Numbers

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Add Two Numbers

Total Accepted: 13127 Total
Submissions: 58280

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

题意:给定两个代表数字的链表,每个节点里存放一个digit,数字是逆方向的,将这两个链表相加起来

思路:

1.i, j遍历l1,l2至最长,短的补零

2..设置一个进位变量c, 第i次遍历 l1,l2,c的和除以10进位,mod10留在这一位

3.出循环后还要检查是不是还有进位

复杂度:O(m+n), 空间O(m+n)

相关题目:

Add Binary

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:

    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
    	ListNode *result = NULL, *cur = NULL;
    	int c = 0, s1 = 0, s2 = 0;

    	for(ListNode *i = l1; i != NULL; i = i->next) s1++;
    	for(ListNode *i = l2; i != NULL; i = i->next) s2++;

    	for(ListNode *i =l1, *j = l2; i != NULL || j != NULL; ){
    		int val1 = i == NULL ? 0 : i->val;
    		int val2 = j == NULL ? 0 : j->val;
    		int r = (val1 + val2 + c) % 10;
    		c = (val1 + val2 + c) / 10;
    		if(result == NULL) cur = (result = new ListNode(r));
    		else {
    			cur->next = new ListNode(r);
    			cur = cur->next;
    		}
    		//
    		if(i != NULL) i = i->next;
    		if(j != NULL) j = j->next;
    	}

    	if(c) cur->next = new ListNode(c);
    	return result;
    }
};

Leetcode 线性表 数 Add Two Numbers

时间: 2024-10-16 20:11:30

Leetcode 线性表 数 Add Two Numbers的相关文章

Leetcode 线性表 Two Sum

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Two Sum Total Accepted: 19206 Total Submissions: 103959 Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of

Leetcode 线性表 Remove Duplicates from Sorted List II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted List II Total Accepted: 10702 Total Submissions: 43714 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from th

Leetcode 线性表 Swap Nodes in Pairs

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Total Submissions: 39302 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte

Leetcode 线性表 Remove Duplicates from Sorted Array

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array Total Accepted: 14789 Total Submissions: 46601 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new l

Leetcode 线性表 Remove Duplicates from Sorted List

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted List Total Accepted: 14961 Total Submissions: 43772 Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1

Leetcode 线性表 Remove Element

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Element Total Accepted: 13840 Total Submissions: 42676 Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be change

Leetcode 线性表 Linked List Cycle

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Linked List Cycle Total Accepted: 17041 Total Submissions: 48975 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 题意:判断一个链表中是否有环 思路:快慢

Leetcode 线性表 Remove Nth Node From End of List

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Nth Node From End of List Total Accepted: 12160 Total Submissions: 41280 Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1