博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode - Medium - Add Two Numbers
阅读量:5938 次
发布时间:2019-06-19

本文共 2779 字,大约阅读时间需要 9 分钟。

hot3.png

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 -> 8Explanation: 342 + 465 = 807.

thinking

加法运算规则,指针的操作

solution

public class AddTwoNumbers {	public class ListNode {		int val;		ListNode next;		ListNode(int x) {			val = x;		}				[@Override](https://my.oschina.net/u/1162528)		public String toString() {			StringBuilder sb = new StringBuilder();			sb.append(val);			ListNode pointer = next;			while(pointer != null) {				sb.append("->");				sb.append(pointer.val);				pointer = pointer.next;			}						return sb.toString();		}				/* 		 * 借助toString 判断链表相等		 * [@see](https://my.oschina.net/weimingwei) java.lang.Object#toString()		 */		[@Override](https://my.oschina.net/u/1162528)		public boolean equals(Object obj) {			if(!(obj instanceof ListNode))				return false;						return this.toString().equals(((ListNode)obj).toString());		}	}	//假设l1,l2都是合法参数,返回新的链表	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {				//进位		int carry = 0;				ListNode head = null, tail = null;				while(l1 != null || l2 != null) {						int a = l1 != null ? l1.val : 0 ;			int b = l2 != null ? l2.val : 0;						int sum = a + b + carry;			carry = sum / 10;						ListNode tmp = new ListNode(sum % 10);						if(head == null && tail == null) {				head = tail = tmp;			}else {				tail.next = tmp;				tail = tail.next;			}						if(l1 != null) {				l1 = l1.next;			}			if(l2 != null) {				l2 = l2.next;			}		}				if(carry != 0) {			tail.next = new ListNode(carry);			tail = tail.next;		}				return head;	}		//辅助,非负数逆序队列,用队列实现	public ListNode transform(int from) {				//个位		int digit = from % 10;		ListNode tail = new ListNode(digit);		ListNode head = tail;		from = (from - digit) / 10; 				while(from > 0) {			digit = from % 10;			tail.next = new ListNode(digit);			tail = tail.next;			from = (from - digit) / 10; 		}				return head;	}		@//Test	public void testTransform() {				assertEquals("1", transform(1).toString());		assertEquals("9->8->7->6->9->8->7->6", transform(67896789).toString());		assertEquals("3->2->1", transform(123).toString());		assertEquals(transform(123).toString(), transform(123).toString());	}		@//Test	public void testAddTwoNumbers() {		assertEquals("2->2->2", addTwoNumbers(transform(111), transform(111)).toString());		assertEquals("7->0->8", addTwoNumbers(transform(342), transform(465)).toString());		assertEquals("0->0->0->1", addTwoNumbers(transform(990), transform(10)).toString());	}}

reference

转载于:https://my.oschina.net/jallenkwong/blog/1795530

你可能感兴趣的文章
度量时间差
查看>>
apache prefork模式优化错误
查看>>
通过jsp请求Servlet来操作HBASE
查看>>
JS页面刷新保持数据不丢失
查看>>
清橙A1202&Bzoj2201:彩色圆环
查看>>
使用data pump工具的准备
查看>>
springMVC---级联属性
查看>>
get和post区别
查看>>
crontab执行shell脚本日志中出现乱码
查看>>
cmd.exe启动参数说明
查看>>
《随笔记录》20170310
查看>>
网站分析系统
查看>>
从零开始来看一下Java泛型的设计
查看>>
Shell编程基础
查看>>
Shell之Sed常用用法
查看>>
3.1
查看>>
校验表单如何摆脱 if else ?
查看>>
JS敏感信息泄露:不容忽视的WEB漏洞
查看>>
让我们荡起双桨,Android 小船波浪动画
查看>>
分布式memcached服务器代理magent安装配置(CentOS6.6)
查看>>