Linked List - Add Two Numbers II

2017-04-26  本文已影响75人  衣忌破

https://leetcode.com/problems/add-two-numbers-ii/#/description
我的解法思路是想将两个链表反转后按顺序相加,然后再将结果反转

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
         ListNode* node = new ListNode(0);
         ListNode* tmp = new ListNode(0);
         tmp->next = node;
         l1 = reverseList(l1);
         l2 = reverseList(l2);
         
         int flag = 0;
         while(l1!=NULL || l2!=NULL){
             int digit = 0;
             int result = 0;
             
             if(l1 == NULL){
                 result = 0 + l2->val;
             }else if(l2 == NULL){
                 result = 0 + l1->val;
             }else{
                 result = l1->val + l2->val;
             }
                 
             if(flag == 1){
                result = result+1;
                flag = 0;
             }
             
             if(result >= 10){
                 flag = 1;
                 digit = result - 10;
             }else{
                 digit = result;
             }
             
             if(l1!=NULL){
                  l1 = l1->next;
             }
            
            if(l2!=NULL){
                 l2 = l2->next;
             }
             
             tmp->next->next = new ListNode(0);
             tmp->next->next->val = digit;
             tmp->next = tmp->next->next;
         }
         
         if(flag == 1){
             tmp->next->next = new ListNode(0);
             tmp->next->next->val = 1;
             return reverseList(node->next);
         }
         return reverseList(node->next);
    }
    
    ListNode* reverseList(ListNode* head) {
        if (!head || !(head -> next)) return head;
        ListNode* node = reverseList(head -> next);
        head -> next -> next = head;
        head -> next = NULL;
        return node; 
    }
};

leetcode上提供的另外一种解法主要分为两步,第一步开始不反转链表从链表末尾开始将链表相加,并将结果(此时结果可能是两位数,没有对进位进行处理)添加到新链表的头部,这步结束后相当于将链表相加并反转。第二步对第一步的进位进行处理。

 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int n1 = 0, n2 = 0, carry = 0;
        ListNode *curr1 = l1, *curr2 = l2, *res = NULL;
        while( curr1 ){ curr1=curr1->next; n1++; }
        while( curr2 ){ curr2=curr2->next; n2++; } 
        curr1 = l1; curr2 = l2;
        while( n1 > 0 && n2 > 0){
            int sum = 0;
            if( n1 >= n2 ){ sum += curr1->val; curr1=curr1->next; n1--;}
            if( n2 > n1 ){ sum += curr2->val; curr2=curr2->next; n2--;}
            res = addToFront( sum, res );
        }
        curr1 = res; res = NULL;
        while( curr1 ){
            curr1->val += carry; carry = curr1->val/10;
            res = addToFront( curr1->val%10, res );
            curr2 = curr1; 
            curr1 = curr1->next;
            delete curr2;
        }
        if( carry ) res = addToFront( 1, res );
        return res;
    }
    ListNode* addToFront( int val, ListNode* head ){
        ListNode* temp = new ListNode(val);
        temp->next = head;
        return temp;
    }
上一篇 下一篇

猜你喜欢

热点阅读