题目: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

 #include "stdafx.h"
#include <malloc.h>
#include <iostream>
using namespace std;
typedef struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {} }*Lnode;
Lnode create() //尾插法建立链表
{
Lnode head,q,r;
int temp;
head = (struct ListNode *)malloc(sizeof(Lnode));
head->next = NULL;
r = head;
cin >> temp;
while (temp!=-)//输入-1,创建链表结束
{
q = (struct ListNode *)malloc(sizeof(Lnode));
q->val = temp;
r->next = q;
r = q;
cin >> temp;
}
r->next = NULL;
return head;
}
void print(Lnode h) //打印链表
{
Lnode p=h->next;
while (p)
{
cout << p->val<<endl;
p = p->next;
}
}
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{
int flag = ;
ListNode * p,*r;
ListNode * h = (struct ListNode *)malloc(sizeof(ListNode *));
h->next = NULL;
r = h;
int num;
if (l1 == NULL) return l1;
if (l2 == NULL) return l2;
while (l1&&l2)
{
num = l1->val + l2->val + flag;
if (num<)
{
p = (struct ListNode *)malloc(sizeof(ListNode *));
p->val = num;
r->next = p;
r = p;
flag = ;
}
else //如果两位数相加大于10,则向前进一位
{
p = (struct ListNode *)malloc(sizeof(ListNode *));
p->val = num-;
r->next = p;
r = p;
flag = ;
}
l1 = l1->next;
l2 = l2->next;
}
while (l1)
{
num = l1->val + flag;
if (num<)
{
p = (struct ListNode *)malloc(sizeof(ListNode *));
p->val = num;
r->next = p;
r = p;
flag = ;
}
else
{
p = (struct ListNode *)malloc(sizeof(ListNode *));
p->val = num - ;
r->next = p;
r = p;
flag = ;
}
l1 = l1->next; } while (l2)
{
num = l2->val + flag;
if (num<)
{
p = (struct ListNode *)malloc(sizeof(ListNode *));
p->val = num;
r->next = p;
r = p;
flag = ;
}
else
{
p = (struct ListNode *)malloc(sizeof(ListNode *));
p->val = num - ;
r->next = p;
r = p;
flag = ;
}
l2 = l2->next; }
if (flag) //最后再判断一次判断是否有进位
{
p = (struct ListNode *)malloc(sizeof(ListNode *));
p->val = ;
r->next = p;
r = p;
}
r->next = NULL;
return h->next;
}
int _tmain(int argc, _TCHAR* argv[]) //测试函数
{
Lnode t1,t2,t;
t1 = create();
t2 = create();
t = addTwoNumbers(t1->next,t2->next);
print(t);
return ;
}

  Java版本:

public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2)
{
if(l1==null)
return l2;
if(l2==null)
return l1;
ListNode head=new ListNode(0);
head.next=null;
int tag=0;//进位标志
if(l1.val+l2.val<10)
{
head.val=l1.val+l2.val;
}
else
{
head.val=l1.val+l2.val-10;
tag=1;
}
ListNode p=head;
ListNode node1=l1.next;
ListNode node2=l2.next;
while(node1!=null||node2!=null)
{
int sum;
int result;
ListNode node=new ListNode(0);
node.next=null;
p.next=node;
p=node;
if(node1!=null&&node2!=null)
{
sum=node1.val+node2.val;
result=sum+tag;
if(result<10)
{
node.val=result;
tag=0;
}
else
{
node.val=result-10;
tag=1;
}
node1=node1.next;
node2=node2.next;
}
else if(node1==null&&node2!=null)
{
sum=node2.val;
result=sum+tag;
if(result<10)
{
node.val=result;
tag=0;
}
else
{
node.val=result-10;
tag=1;
}
node2=node2.next;
}
else if(node1!=null&&node2==null)
{
sum=node1.val;
result=sum+tag;
if(result<10)
{
node.val=result;
tag=0;
}
else
{
node.val=result-10;
tag=1;
}
node1=node1.next;
continue;
}
}
if(node1==null&&node2==null&&tag==1)
{
ListNode node=new ListNode(1);
node.next=null;
p.next=node;
p=node;
}
return head;
}
}

【LeetCode OJ】Add Two Numbers的更多相关文章

  1. 【LeetCode练习题】Add Two Numbers

    链表相加 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  2. 【LeetCode OJ】Longest Consecutive Sequence

    Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...

  3. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  4. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  5. 【LeetCode OJ】Sum Root to Leaf Numbers

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...

  6. 【LeetCode OJ】Symmetric Tree

    Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...

  7. 【LeetCode OJ】Minimum Depth of Binary Tree

    Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...

  8. 【LeetCode OJ】Path Sum

    Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...

  9. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

随机推荐

  1. python公司内部语言规范与语言风格

    一.python语言规范 1.1导入 Tip: 仅对包和模块使用导入 定义: 模块间共享代码的重用机制. 优点: 命名空间管理约定十分简单.每个标识符的源都用一种一致的方式指示.x.obj 表示obj ...

  2. 查看 共享内存 的命令 ipcrm、ipcs

    ipcrm 命令 移除一个消息对象.或者共享内存段,或者一个信号集,同时会将与ipc对象相关链的数据也一起移除.当然,只有超级管理员,或者ipc对象的创建者才有这项权利啦 ipcrm用法 ipcrm ...

  3. Java数组超出范围时如何处理多个异常?

    在Java编程中,数组超出范围时如何处理多个异常? 此示例显示如何使用System类的System.err.println()方法处理多个异常方法. package com.yiibai; publi ...

  4. JDBC PrepareStatement对象执行批量处理实例

    以下是使用PrepareStatement对象进行批处理的典型步骤顺序 - 使用占位符创建SQL语句. 使用prepareStatement()方法创建PrepareStatement对象. 使用se ...

  5. pyhont备份php代码脚本

    #!/usr/bin/env python # encoding: utf-8 import time import os import sys import logging #create logg ...

  6. Windows版Jenkins+SVN+Maven自动化部署环境搭建【转】

    前言 因今年公司新产品线较多,为了降低耦合,达到业务分离.重用,提高内部开发效率的目的,采用了基于服务组件.前后端分离的架构体系.与之前传统单应用架构相比,系统部署.配置更加复杂,为了能够频繁地将软件 ...

  7. Mac或者linux下登陆到linux上的SFTP

    登陆 sftp  -i  密钥路径  用户@ip ➜  ~ sftp -i Desktop/aliyun.pem root@39.106.30.1 Connected to 39.106.30.1 上 ...

  8. 解决iredmail下postfix发送邮件时报错[need fully-qualified hostname]

    iredmail配置好后,尝试从一客户端的OE中发送邮件,结果报错,在mail主机中查看tail /var/log/maillog,发现如下错误信息: Oct :: mail2 postfix/smt ...

  9. Array常用函数收藏

    1.isArray 语法:Array.isArray(obj) 说明:判断一个对象是否是数组. 例如: Array.isArray([]); Array.isArray([1]); Array.isA ...

  10. tomcat部署会碰到的问题

    nginx转发丢失session的问题 问题描述: 在Nginx配置反向代理的时候,需要将一个特定的URL请求转发到一个带有页面的Web后台管理系统.部署完成之后发现该后管系统无法正常登陆,输入正确账 ...