题目要求:
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
需要注意以下几点:
1.因为存储是反过来的,即数字342存成2->4->3,所以要注意进位是向后的;
2.链表l1或l2为空时,直接返回,这是边界条件,省掉多余的操作;
3.链表l1和l2长度可能不同,因此要注意处理某个链表剩余的高位;
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
需要注意以下几点:
1.因为存储是反过来的,即数字342存成2->4->3,所以要注意进位是向后的;
2.链表l1或l2为空时,直接返回,这是边界条件,省掉多余的操作;
3.链表l1和l2长度可能不同,因此要注意处理某个链表剩余的高位;
4.2个数相加,可能会产生最高位的进位,因此要注意在完成以上1-3的操作后,判断进位是否为0,不为0则需要增加结点存储最高位的进位。
ListNode的头文件
//
// ListNode.h
// ListNode_addTwoNumbers
//
// Created by bcc_cae on 16/3/30.
// Copyright © 2016年 bcc_cae. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ListNode : NSObject
//初始化节点
-(ListNode *)initWithNode:(int)value;
//创建链表
- (ListNode *)createLinkList:(int)n;
//输出链表节点
- (void)printLinkList:(ListNode *)node;
//两个链表数相加
-(ListNode *)addTwoNumbers:(ListNode *)node1 node2:(ListNode *)node2;
@end
ListNode实现文件
//
// ListNode.m
// ListNode_addTwoNumbers
//
// Created by bcc_cae on 16/3/30.
// Copyright © 2016年 bcc_cae. All rights reserved.
//
#import "ListNode.h"
//@class ListNode;
@interface ListNode (){
int data;
ListNode *head;
ListNode *next;
}
@end
@implementation ListNode
/**
* 初始化链表节点
*
* @param value <#value description#>
*
* @return return value description
*/
-(ListNode *)initWithNode:(int)value
{
ListNode *node = [[ListNode alloc] init];
node->data = value;
return node;
}
/**
* 创建链表
*
* @param n代表节点个数
*
* @return <#return value description#>
*/
- (ListNode *)createLinkList:(int)n
{
head = [[ListNode alloc] init];
head->next = nil;
ListNode *ptr = head;
for ( int i=1; i<=n; ++i) {
ListNode *node = [[ListNode alloc] init];
node->data = arc4random()%9; //随机产生0~9的数
ptr->next = node;
ptr = node;
}
return head;
}
/**
* 打印节点
*
* @param node <#node description#>
*/
- (void)printLinkList:(ListNode *)node{
ListNode *ptr = node->next;
while (ptr != nil) {
NSLog(@"%d",ptr->data);
ptr = ptr->next;
}
}
/**
* 两个链表相加
*
* @param node1 <#node1 description#>
* @param node2 <#node2 description#>
*
* @return <#return value description#>
*/
-(ListNode *)addTwoNumbers:(ListNode *)node1 node2:(ListNode *)node2
{
//头结点
ListNode *head = [self initWithNode:0];
ListNode *preNode = head;
//代表进位
int add = 0;
ListNode *b1=node1,*b2=node2;
while (b1!=NULL||b2!=NULL||add>0) {
ListNode *node = [self initWithNode:0];
int val1 = b1?b1->data:0;
int val2 = b2?b2->data:0;
b1 = b1?b1->next:NULL;
b2 = b2?b2->next:NULL;
node->data = (val1+val2+add)%10;
add = (val1 + val2 + add) /10;
preNode->next = node;
preNode = node;
}
return head->next;
}
@end
//
// ViewController.m
// ListNode_addTwoNumbers
//
// Created by bcc_cae on 16/3/30.
// Copyright © 2016年 bcc_cae. All rights reserved.
//
#import "ViewController.h"
#import "ListNode.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
ListNode *node = [[ListNode alloc] init];
ListNode *node1 = [node createLinkList:3];
[node printLinkList:node1];
ListNode *node2 = [node createLinkList:5];
[node2 printLinkList:node2];
ListNode *node3 = [node addTwoNumbers:node1 node2:node2];
[node printLinkList:node3];
}
@end