leetcode 第二题 Add Two Numbers c语言和java版

你被给予两个非空的链表代表两个非负的整形数,并且逆序储存,每个节点包含一位数字,将两数相加然后返回一个储存了它们和的每位数字的逆序链表。

输入 2 ->4-> 3 + 5->6->4

输出7->0->8

因为 342 + 465 =   807;

说一下思路及要点

首先我用Java写,考虑用栈,后来发现用栈其实是错误的,应该使用队列,于是我就使用了队列的api,后来我用c写时发现根本无需显式的使用栈,直接写就可以,不过这题链表可能会很长,直接使用整形变量是行不通的,考虑biginteger api或者直接用应用大数计算的方法,即化数为数组,c代码要注意将最后将node->next置为0,不然会无限循环,其实任何时候都应如此,养成好习惯很重要。

c代码

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
int a[100];
    int c = 0;
    int d = 0;
    int i = 0;
    int yu = 0;
while(l1 != 0 || l2 != 0 || yu != 0){
    c = 0;
    d = 0;
    if(l1 != 0){
        c = l1->val;
        l1 = l1->next;
    }else{
        c = 0;
    }

    if(l2 != 0){
        d = l2->val;
        l2 = l2->next;
    }else{
        d = 0;
    }
    a[i] = (yu + c + d) % 10;
    yu = (yu + c + d) / 10;
    i++;
}
  int j = 0;
  struct ListNode* head = 0;
  struct ListNode * node = 0;
    for(j = 0;j < i;j++){
    if(head == 0){
        node = malloc(sizeof(struct ListNode) * 20);
        head = node;
        head->next = 0;
        head->val = a[j];
    }else{
        node -> next= malloc(sizeof(struct ListNode) * 20);
        node = node->next;
        node->next = 0;
        node->val = a[j];
        
    }
  }
    printf("%d\n",head->val);
  return head;
}

Java代码   走了许多弯路。。。。


		
		ListNode l = new ListNode(1);
		ListNode l2 = new ListNode(9);
		ListNode l3 = new ListNode(9);
        l.next = l2;
        l2.next = l3;
        ListNode l4 = new ListNode(9);
        l3.next = l4;
        ListNode l5 = new ListNode(9);//102;
	    l4.next = l5;
	    ListNode l6 = new ListNode(9);//102;
	    l5.next = l6;
	    ListNode l8 = new ListNode(9);//102;
	    
	    ListNode l7 = new Test2().addTwoNumbers(l, l8);
	    while(l7 != null) {
			//a.push(l1.val);
	    	System.out.print(l7.val);
	    	l7 = l7.next;
		}
	}
	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		
	Queue<Integer> a = new LinkedList<Integer>();
	Queue<Integer> b = new LinkedList<Integer>();
	while(l1 != null || l2 != null) {
		if(l1 != null) {
			a.add(l1.val);
			l1 = l1.next;
		}
		
		if(l2 != null) {
			b.add(l2.val);
			l2 = l2.next;
			
		}
	}
	int i = 0;
	long s1 = 0,s2 = 0;
	int[] shuzu = new int[100];
	int yu = 0;
    while(!a.isEmpty() || !b.isEmpty() || yu != 0) {
		int c = 0,d = 0;
		if(!a.isEmpty()) {
			c = a.poll();
			//s1 = s1 * i + c;
		}else {
			c = 0;
		}
		
		if(!b.isEmpty()) {//96
			d = b.poll();//75  16
			//s2 = s2 * i + d;
		}else {
			d = 0;
		}
		shuzu[i] = (c + d + yu) % 10;
        yu = (yu + c + d)/10;
		i++;
		
	}
	//System.out.println(s1);
	//System.out.println(s2);
	
	ListNode head = null;
	ListNode l = null;
	for (int j = 0;j < i;j++) {
		int s = shuzu[j];
		if(head == null) {
			l = new ListNode(s);
			head = l;
		}else{
			 l.next = new ListNode(s);
			 l = l.next;
		}
		
		
		
	}
		
		return head;  
    }

java代码    官方大佬写的就是这么飘逸。。不过似乎没有c

public 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;
}

猜你喜欢

转载自blog.csdn.net/qq_39668086/article/details/81489734