LeetCode 2

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 it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

简单描述:两个链表按顺序相加,大于10的取10的余数(%10),向下位进1。

注意点:当最后一个节点的和大于等于10时,需要增加一个节点。

C语言

#include<stdio.h>
#include<malloc.h>

struct ListNode{
    int val;
    struct ListNode *next;
 };
 


//创建NULL节点 
struct ListNode* newNode(int a){
    struct ListNode* newListNode = malloc(sizeof(struct ListNode));
    if(newListNode==NULL){
        return NULL; 
    }
    newListNode->val = a;
    newListNode->next = NULL;
    return newListNode; 
}

//创建 指定长度和数据的链表 
struct ListNode* createListNode(long array[],int n){
    int i;
    struct ListNode *head,*p;
    head = p = newNode(array[0]);
    
    for(i=1;i<n;i++){
        p->next = newNode(array[i]);
        p = p->next;
    }
    return head;
}

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode * p1,*p2;
    p1 = l1;
    p2 = l2;
    
    struct ListNode* head;
    head = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* p3 = head;
  
    int  t =0;
    
    while(p1!=NULL || p2!=NULL){

        int a1 = p1!=NULL? p1->val : 0;
        int a2 = p2!=NULL? p2->val : 0;
        int sum = a1+a2+t;
        t = sum/10;
 
        p3->next = (struct ListNode*)malloc(sizeof(struct ListNode));
        p3 = p3->next;
        p3->val = sum%10;
        
        if(p1!=NULL){
            p1 = p1->next;
        }
        if(p2!=NULL){
            p2 = p2->next; 
        }
    }
    
    if(t>0){
        p3->next = (struct ListNode*)malloc(sizeof(struct ListNode)); 
        p3 = p3->next;
        p3->val = t;
    }
    p3->next=NULL;
    return head->next;
}

//测试
int main(){
    struct ListNode *l1,*l2;
//  int a1[] = {2,4,3};
//  int a2[] = {5,6,4};
//  l1 = createListNode(a1,3);
//  l2 = createListNode(a2,3);
//  long a1[] = {9};
//  long a2[] = {1,9,9,9,9,9,9,9,9,9};
//  l1 = createListNode(a1,1);
//  l2 = createListNode(a2,10);
    long a1[] = {5};
    long a2[] = {5};
    l1 = createListNode(a1,1);
    l2 = createListNode(a2,1);
    
    struct ListNode *p = addTwoNumbers(l1,l2); 
    while(p!=NULL){
        printf("->%d",p->val);
        p = p->next;
    }
    return 0;
}

复杂度分析:

时间复杂度:O(max(m,n)), 只有一个while语句,传入的2个链表的最大长度决定循环的次数。

空间复杂度:O(max(m,n)),传入的2个链表的最大空间决定新建链表的最大空间,如果最后节点和大于等于10,空间复杂度为O(max(m,n))+1。

Java实现

public class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { 
            val = x;
      }
 }

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode head= new ListNode(0);
    ListNode p = l1, q = l2, curr = head;
    int carry = 0;
    while (p != null || q != null) {
        int x = (p != null) ? p.val : 0;
        int y = (q != null) ? q.val : 0;
        int sum = carry + x + y;
        carry = sum / 10;
        curr.next = new ListNode(sum % 10);
        curr = curr.next;
        if (p != null) p = p.next;
        if (q != null) q = q.next;
    }
    if (carry > 0) {
        curr.next = new ListNode(carry);
    }
    return head.next;
}

Java版和C语言版基本一样。

    原文作者:旋哥
    原文地址: https://www.jianshu.com/p/6a9adff0ba30
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞