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)

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
原创粉丝点击