题干:

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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

注:

题目中链表的定义为

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

分析:

  题干中链表中的数是倒序存储的,题干中给的例子其实是:342+465=807。

  我们定义一个节点dummyHead来指向结果的头结点,然后定义三个节点p,q,curr分别用来记录l1当前节点,l2当前节点,结果当前节点。

  定义int carry来记录是否有进位。

public static 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 = 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 dummyHead.next;
}

面试题:Add Two Numbers(模拟单链表)的更多相关文章

  1. PHP模拟单链表的数据结构

    <?php /*** * 单链表 */ //节点,下标,节点名称,下一个节点的地址 class Node { public $id; public $name; public $next; pu ...

  2. 2.Add Two Numbers-两个单链表相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. LeetCode 2 Add Two Numbers 模拟,读题 难度:0

    https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-n ...

  4. 2. Add Two Numbers(2个链表相加)

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  5. python算法双指针问题:使用列表和数组模拟单链表

    这个很多基础算法,python已内部实现了. 所以,要想自己实现链表这些功能时, 反而需要自己来构造链表的数据结构. 当然,这是python灵活之处, 也是python性能表达不如意的来源. valu ...

  6. LeetCode 445. Add Two Numbers II(链表求和)

    题意:两个非空链表求和,这两个链表所表示的数字没有前导零,要求不能修改原链表,如反转链表. 分析:用stack分别存两个链表的数字,然后从低位开始边求和边重新构造链表. Input: (7 -> ...

  7. Add Two Numbers ,使用链表参数

    # Definition for singly-linked list. class ListNode(object): def __init__(self, x): self.val = x sel ...

  8. LeetCode 单链表专题 (一)

    目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...

  9. java实现单链表的增删改以及排序

    使用java代码模拟单链表的增删改以及排序功能 代码如下: package com.seizedays.linked_list; public class SingleLinkedListDemo { ...

随机推荐

  1. 【bzoj4994】[Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组

    题目描述 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 样例输入 4 3 2 4 4 1 3 2 1 样例输 ...

  2. 2.LXC和namespace介绍

    lxc介绍 LXC又名Linux container,是一种虚拟化的解决方案,这种是内核级的虚拟化.(主流的解决方案Xen ,KVM, LXC) Linux Container容器是一种内核虚拟化技术 ...

  3. Nano

    Nano命令指南 今天在输命令时,无意中输入了nano,对这个命令不太熟悉,结果不知道如何才能退出,保存,赶快查了一下资料,原来是这样的啊. 打开文件与新建文件 使用nano打开或新建文件,只需键入: ...

  4. 转 python数据类型详解

    python数据类型详解 目录 1.字符串 2.布尔类型 3.整数 4.浮点数 5.数字 6.列表 7.元组 8.字典 9.日期 1.字符串 1.1.如何在Python中使用字符串 a.使用单引号(' ...

  5. Servlet 2.4 规范之第七篇:过滤器

    过滤器是一套java组件,用于在请求—>资源—>应答的这一过程中即时转换处理负载和头信息. 本章讲述了Servlet 2.4 API中一些类和方法,这些类和方法提供了一套轻量级框架用于过滤 ...

  6. MVC5的坑

    事情是这样的,今天在写一个功能模块的时候,创建的方法,到controller里,死活为null 以前从没出现这种情况啊,但是区别是这个代码是多层跳转进来的,难道是页面跳转太多,还记得之前的model, ...

  7. codeforces-574B

    题目连接:http://codeforces.com/contest/574/problem/B B. Bear and Three Musketeers time limit per test 2 ...

  8. 51nod 1182 完美字符串【字符串排序+哈希】

    1182 完美字符串 题目来源: Facebook Hacker Cup选拔 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 约翰认为字符串的完美度等 ...

  9. Python_Tips[5] -> 可变数据类型作为初始化形参

    可变数据类型作为初始化形参 / Mutable Parameter as Init Formal-para 由于在Python中,没有类似C语言的static静态参数,因此当一个函数需要一个只初始化一 ...

  10. Nginx配置文件分析

    #user nobody; #启动进程数,即启动ngnix服务的个数,通常设置和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log logs/e ...