2.两数相加

1.oc

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

//
//  ViewController.m

#import "ViewController.h"
#import "ListNode.h"
#import "NSString+Reverse.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    ListNode *l1 = [[ListNode alloc]initWithValue:@"2"];
    ListNode *l2 = [[ListNode alloc]initWithValue:@"4"];
    ListNode *l3 = [[ListNode alloc]initWithValue:@"3"];
    l1.next = l2;
    l2.next = l3;
    
    ListNode *l4 = [[ListNode alloc]initWithValue:@"5"];
    ListNode *l5 = [[ListNode alloc]initWithValue:@"6"];
    ListNode *l6 = [[ListNode alloc]initWithValue:@"4"];
    l4.next = l5;
    l5.next = l6;
    ListNode *l7 = [self addTwoNum:l1 :l4];
    while (l7) {
       
        NSLog(@"%@",l7.val);
        l7 = l7.next;
    }
}
- (ListNode *)addTwoNum:(ListNode *)l1 :(ListNode *)l2  {
    
    NSString *str1 = [[NSString alloc]init];
   
    while (l1) {
        str1 = [str1 stringByAppendingString:l1.val];
        l1 = l1.next;
       
    }
    
    NSString *str2 = [[NSString alloc]init];
    while (l2) {
        str2 = [str2 stringByAppendingString:l2.val];
        l2 = l2.next;
    }
    
    str1 = [str1 stringByReversed];
    str2 = [str2 stringByReversed];
    int result = [str1 intValue] + [str2 intValue];
    NSString *str = [NSString stringWithFormat:@"%d",result];
    str = [str stringByReversed];
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < str.length; i++) {
        unichar c = [str characterAtIndex:i];
        NSString *s = [NSString stringWithCharacters:&c length:1];
        [arr addObject:s];
    }
    
    ListNode *listResult = [[ListNode alloc]initWithValue:arr[0]];
    ListNode *p = listResult;
    int i = 1;
    while (i < arr.count) {
        ListNode *l = [[ListNode alloc]initWithValue:arr[i]];
        p.next = l;
        p = p.next;
        i++;
    }

    return listResult;
  
}


@end
//
//  ListNode.h


#import 

NS_ASSUME_NONNULL_BEGIN

@interface ListNode : NSObject
@property(nonatomic,copy) NSString *val;
@property(nonatomic,strong) ListNode *next;
-(instancetype)initWithValue:(NSString *) val;
@end



NS_ASSUME_NONNULL_END
//
//  ListNode.m


#import "ListNode.h"

@implementation ListNode
-(instancetype)initWithValue:(NSString *)val {
    self = [super init];
    if (self) {
        self.val = val;
    }
    return self;
}

-(id)copyWithZone:(NSZone *)zone {
    ListNode *newNode = [[ListNode allocWithZone:zone] init];
    newNode.val = self.val;
    newNode.next = self.next;
    return newNode;
}
@end
//
//  NSString+Reverse.m


#import "NSString+Reverse.h"

@implementation NSString (Reverse)
- (NSString *)stringByReversed {
    NSMutableString *reverseString = [NSMutableString string];
    for (NSInteger i = self.length -1 ; i >= 0; i--) {
        unichar c = [self characterAtIndex:i];
        NSString *s = [NSString stringWithCharacters:&c length:1];
        [reverseString appendString:s];
    }
    return reverseString;
}
@end

2.java

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(0);
        ListNode p = l1, q = l2, curr = dummyHead;
        int carry = 0;
        while (p != null || q != null) {
            int x = (p != null) ? p.val : 0;
            int y = (q != null) ? q.val : 0;
            int sum = x + y + carry;
            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 dummyHead.next;
    }
}

3.swift

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public var val: Int
 *     public var next: ListNode?
 *     public init(_ val: Int) {
 *         self.val = val
 *         self.next = nil
 *     }
 * }
 */
class Solution {
    func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
        let head = ListNode(0);
        var flag = 0;
        var num1 = l1;
        var num2 = l2;

        var p = head;
        var dummy = ListNode(0);

        while (num1 != nil || num2 != nil) {
            let x = (num1 != nil) ? num1!.val : 0;
            let y = (num2 != nil) ? num2!.val : 0
            p.val = (x + y + flag > 9) ? x + y - 10 + flag : x + y + flag;
            flag = (x + y + flag > 9) ? 1 : 0;

            let q = ListNode(0);
            p.next = q;
            dummy = p;
            p = p.next!
            if num1 != nil {
                num1 = num1!.next;
            }
            if num2 != nil {
                num2 = num2!.next;
            }
        }
        if flag > 0 {
            p.val = 1;
        } else {
            dummy.next = nil;
        }
        return head;

    }
}

你可能感兴趣的:(2.两数相加)