careercup-链表 2.5

2.5 给定两个用链表表示的整数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表首部。编写函数对这两个整数求和,并用链表形式返回结果。

示例:

输入: (7->1->6)+(5->9->2),即617+295.

输出:2->1->9,即912.

进阶:

假设这些数位是正向存放的

示例:

输入:(6->1->7)+(2->9->5),即617+295.

输出:9->1->2,即912.

逆向存放时,C++实现:

#include<iostream>
#include<new>
using namespace std;

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x):val(x),next(NULL) {}
};

void createList(ListNode *&L,int arr[],int n)
{
    int i;
    ListNode *p=NULL;
    for(i=0; i<n; i++)
    {
        ListNode *tmp=new ListNode(arr[i]);
        if(L==NULL)
        {
            L=tmp;
            p=tmp;
        }
        else
        {
            p->next=tmp;
            p=tmp;
        }
    }
}

ListNode *merge(ListNode *L1,ListNode *L2)
{
    if(L1==NULL&&L2==NULL)
        return NULL;
    if(L1==NULL||L2==NULL)
        return L1!=NULL?L1:L2;
    ListNode *p=L1;
    ListNode *pre=L1;
    ListNode *q=L2;
    int carry=0;
    while(p&&q)
    {
        int sum=p->val+q->val+carry;
        p->val=sum%10;
        carry=sum/10;
        pre=p;
        p=p->next;
        q=q->next;
    }
    while(p)
    {
        int sum=p->val+carry;
        cout<<sum<<endl;
        p->val=sum%10;
        carry=sum/10;
        pre=p;
        p=p->next;
    }
    ListNode *tmp=NULL;
    while(q)
    {
        int sum=q->val+carry;
        tmp=new ListNode(sum%10);
        carry=sum/10;
        pre->next=tmp;
        pre=tmp;
        q=q->next;
    }
    if(carry==1)
    {
        tmp=new ListNode(carry);
        pre->next=tmp;
    }
    return L1;
}

int main()
{
    ListNode *L1=NULL;
    ListNode *L2=NULL;
    int arr1[3]={7,1,9};
    int arr2[5]={5,9};
    createList(L1,arr1,3);
    createList(L2,arr2,2);
    ListNode *head=merge(L1,L2);
    ListNode *p=head;
    while(p)
    {
        cout<<p->val<<" ";
        p=p->next;
    }
    cout<<endl;
}

正向存放时,可以先利用头插入将两个链表逆转,然后按照上面的过程求和。

时间: 2024-10-10 07:54:45

careercup-链表 2.5的相关文章

CareerCup之2.2 寻找单链表倒数第n个元素

[题目] 原文: 2.2 Implement an algorithm to find the nth to last element of a singly linked list. 译文: 实现一个算法从一个单链表中返回倒数第n个元素. [分析] (1)创建两个指针p1和p2,指向单链表的开始节点. (2)使p2移动n-1个位置,使之指向从头开始的第n个节点.(意思是就是使p1和p2距离n个位置) (3)接下来检查p2 - > = = null 如果yes返回p1的值,否则继续移动p1和 p

CareerCup之2.1无序链表删除重复元素

[题目] 原文: 2.1 Write code to remove duplicates from an unsorted linked list. FOLLOW UP How would you solve this problem if a temporary buffer is not allowed? 译文: 从一个未排序的链表中移除重复的项 进一步地, 如果不允许使用临时的缓存,你如何解决这个问题? [分析] (1)如果可以使用额外的存储空间,我们就开一个数组来保存一个元素的出现情况.

[CareerCup] 2.7 Palindrome Linked List 回文链表

2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome Linked List 回文链表.

[CareerCup] 2.6 Linked List Cycle 单链表中的环

2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.DEFINITIONCircular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the

[CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项

2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this problem if a temporary buffer is not allowed? 这道题让我们移除无序链表中的重复项,在LeetCode中有两道类似的题是Remove Duplicates from Sorted List 移除有序链表中的重复项 和 Remove Duplicates fr

[CareerCup] 2.2 Kth to Last Element of Linked List 链表的倒数第k个元素

2.2 Implement an algorithm to find the kth to last element of a singly linked list. 这道题让我们求链表中倒数第k个元素,LeetCode中相类似的题目有Kth Largest Element in an Array 数组中第k大的数字 和 Kth Smallest Element in a BST 二叉搜索树中的第K小的元素.但那两道题和这题又不一样,首先这道题是要在链表中操作,链表的特点就是不能通过下标来直接访

[CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点

2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.EXAMPLEInput: the node c from the linked list a->b->c->d->eResult: nothing is returned, but the new linked list looks like a- >

[CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表

4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D, you'll have D linked lists).

[LeetCode]2. Add Two Numbers用链表逆序存储的两个数相加

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

二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST

引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to merge two binary search tree into balanced binary search tree.. Let there be m elements in first tree and n elements in the other tree. Your merge funct