Cracking coding interview(2.4)单向链表相加
来源:互联网 发布:淘宝女装退货率多少 编辑:程序博客网 时间:2024/06/05 22:47
2.4 You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.
EXAMPLE
Input: (3 -> 1 -> 5), (5 -> 9 -> 2)
EXAMPLE
Input: (3 -> 1 -> 5), (5 -> 9 -> 2)
Output: 8 -> 0 -> 8
1.最终可用2种方法解决,递归的,非递归的
目前解法,较为优化,原题已讨论过一次:http://blog.csdn.net/u011559205/article/details/38083359
class Node{int data;Node next;public Node(int data, Node next){this.data = data;this.next = next;}}public class Solution{public static Node addLinkedList1(Node n1, Node n2){if(n1 == null && n2 == null)return null;boolean carry = false;int val = 0;Node p1 = n1, p2 = n2, head = new Node(-1, null);Node p = head;//step 1:for(;p1 != null && p2 != null;p1 = p1.next, p2 = p2.next){if(carry){val = p1.data + p2.data + 1;if(val >= 10){p.data = val % 10;}else{p.data = val;carry = false;}}else{val = p1.data + p2.data;if(val >= 10){p.data = val % 10;carry = true;}else{p.data = val;}}p.next = new Node(-1, null);p = p.next;}//step 2:Node p3 = p1;if(p1 == null)p3 = p2;for(;p3 != null;p3 = p3.next){if(carry){val = p3.data + 1;if(val > 10){p.data = val % 10;}else{p.data = val;carry = false;}}else{p.data = p3.data;}p.next = new Node(-1, null);p = p.next;}//step 3:if(carry){p.data = 1;}else{printNode(head);//p = head;if(head.next == null)//normally, never happenedreturn null;for(;p.next.next != null;p = p.next);p.next = null;}return head;}public static Node addLinkedList2(Node n1, Node n2){return recur(n1, n2, false);}private static Node recur(Node n1, Node n2, boolean carry){if(n1 == null && n2 == null){if(carry)return new Node(1, null);elsereturn null;}else if(carry){int val = 1;Node next1 = null, next2 = null;if(n1 != null){val += n1.data;next1 = n1.next; }if(n2 != null){val += n2.data;next2 = n2.next;}Node n = new Node(-1, null);if(val >= 10){n.data = val % 10;}else{n.data = val;carry = false;}n.next = recur(next1, next2, carry);return n;}else{int val = 0;Node next1 = null, next2 = null;if(n1 != null){val += n1.data;next1 = n1.next;}if(n2 != null){val += n2.data;next2 = n2.next;}Node n = new Node(-1, null);if(val >= 10){n.data = val % 10;carry = true;}else{n.data = val;}n.next = recur(next1, next2, carry);return n;}}//experience is wealth//make better//boolean carry to int carry, Usage: n==null ? null : n.nextpublic static Node addLinkedList3(Node n1, Node n2){return recur2(n1, n2, 0);}private static Node recur2(Node n1, Node n2, int carry){if(n1 == null && n2 == null){if(carry == 1){return new Node(1, null);}elsereturn null;}else{int val = carry;//no matter carry == 0 or 1val += n1 != null ? n1.data : 0;val += n2 != null ? n2.data : 0;Node n = new Node(val % 10, null);n.next = recur2(n1 != null ? n1.next : null,n2 != null ? n2.next : null,val >= 10 ? 1 : 0);return n;}}//simplification for addLinkedList1()//boolean carry -> int carry, eliminate redundancy//recursive to non-recursivepublic static Node addLinkedList4(Node n1, Node n2){if(n1 == null && n2 == null){return null;}int carry = 0, val = 0;Node p1 = n1, p2 = n2, head = null;Node[] p = new Node[2];for(p[1] = head;!(p1 == null && p2 == null && carry == 0);){//p1 = p1.next, p2 = p2.nextval = carry;val += p1 != null ? p1.data : 0;val += p2 != null ? p2.data : 0;carry = val >= 10 ? 1 : 0;//System.out.println("val="+val);//p[1] = new Node(val % 10, null);if(p[0] == null)p[0] = head = p[1];else{p[0].next = p[1];p[0] = p[0].next;}//virable increamentp1 = p1 != null ? p1.next : null;p2 = p2 != null ? p2.next : null;}return head;}private static void printNode(Node head){Node p = head;for(;p != null;p = p.next)System.out.print(p.data+" ");System.out.println();}public static void main(String[] args){Node n1 = new Node(1, null);n1.next = new Node(1, null);n1.next.next = new Node(1, null);Node n2 = new Node(5, null);n2.next = new Node(9, null);n2.next.next = new Node(4, null);n2.next.next.next = new Node(9, null);Solution.printNode(n1);Solution.printNode(n2);Node sum = Solution.addLinkedList4(n1, n2);Solution.printNode(sum);}}
0 0
- Cracking coding interview(2.4)单向链表相加
- Cracking coding interview(2.2)返回单向链表的倒数第n个节点
- Cracking the Coding Interview
- 《Cracking the coding interview》
- Cracking The Coding Interview
- Cracking the coding interview
- Cracking the coding interview
- Cracking the coding interview--Q1
- Cracking the coding interview--Q17
- Cracking the coding interview--Q2
- Cracking the coding interview--Q3
- Cracking the coding interview--Q4
- Cracking the coding interview--Q5
- Cracking the coding interview--Q8
- Cracking the coding interview--Q9
- Cracking the coding interview 题目
- Cracking The Coding Interview 1.1
- Cracking The Coding Interview 1.2
- hive (各种 by)
- Python Flask 在Sina App Engine (SAE)上安家
- [编程之美] PSet2.2 不要被阶乘吓倒
- CCI 1.1 确定一个字符串的字符是否全部不同
- bash比较float类型大小
- Cracking coding interview(2.4)单向链表相加
- mysql安装常见问题(系统找不到指定的文件、发生系统错误 1067 进程意外终止)
- Android四种存储方式: sharedpreference,file,SQlite,contentprovider实现增删改查
- α波是什么?
- activiti-5.16用户手册翻译完毕
- Cocos2d-x加速度计实例:运动的小球
- 什么是α波?
- 【浏览器渲染原理】布局之异步布局和同步布局
- vim c.vim