Linked list OJ (6) Linked list addition (1) Linked list addition (2)

Table of contents

Linked list addition (1)

Linked list addition (2)

Description Two and one have two more inversions


Linked list addition (1)

describe

Given two non-empty linked lists of non-negative integers stored in reverse order, each node stores only one-bit arrays.

Please add the two linked lists and return the linked list in the same way as follows, ensuring that neither of the two numbers starts with 0.

【My solution】It's ridiculously long

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head1 ListNode类 
     * @param head2 ListNode类 
     * @return ListNode类
     */
    ListNode* reverse(ListNode* head)
    {
        ListNode* pre = nullptr, *Next = nullptr;
        while(head)
        {
            Next = head->next;
            head->next = pre;
            pre = head;
            head = Next;
        }
        return pre;
    }
    ListNode* addInList(ListNode* l1, ListNode* l2) {
        // write code here
        l1 = reverse(l1);
        l2 = reverse(l2);
        if (l1->val == 0)
            return l2;
        else if (l2->val == 0)
            return l1;
        ListNode* newhead = new ListNode(0);
        ListNode* p = newhead;
        int step = 0;
        while (l1 && l2) {
            int num = l1->val + l2->val + step;
            if (num >= 10)
                step = 1;
            else
                step = 0;
            ListNode* newnode = new ListNode(num % 10);
            p->next = newnode;
            p = p->next;
            l1 = l1->next;
            l2 = l2->next;
        }
        if (l1) {
            p->next = l1;
            while (l1) {
                l1->val += step;
                if (l1->val >= 10) {
                    step = 1;
                    l1->val %= 10;
                } else
                    step = 0;
                l1 = l1->next;
                p = p->next;
            }
        } else if (l2) {
            p->next = l2;
            while (l2) {
                l2->val += step;
                if (l2->val >= 10) {
                    step = 1;
                    l2->val %= 10;
                } else
                    step = 0;
                l2 = l2->next;
                p = p->next;
            }
        }
        if (step) {
            ListNode* newNode = new ListNode(step);
            p->next = newNode;
        }
        return reverse(newhead->next);
    }
};

 【Answer】

specific methods:

  • Step 1: If any linked list is empty, just return another linked list, because an empty linked list is equivalent to 0, 0 plus any number is 0, including the case where another addend is 0.
  • Step 2: Invert the two linked lists to be added one after another. For the inversion process, refer to the inverted linked list .
  • Step 3: Set the linked list head of the returned linked list, and set carry=0.
  • Step 4: Traverse the two linked lists from the beginning until both linked list nodes are empty and carry is not 1. Each time the value of a linked list node that is not empty is taken out, it is set to 0 if it is empty, and the two numbers are compared with carry Add, and then check whether it is carried, add the result after the carry (modulo 10) to a new linked list node, connect it behind the returned linked list, and continue to traverse backwards.
  • Step 5: Reverse the result linked list before returning.
class Solution {
public:
    ListNode* ListAdd(ListNode* head1, ListNode* head2) {
        ListNode* res = new ListNode(-1);
        ListNode* head = res;
        //进位符号
        int carry = 0;
        //只要某个链表还有或者进位还有
        while(head1 != NULL || head2 != NULL || carry != 0){
            //链表不为空则取其值
            int val1 = head1 == NULL ? 0 : head1->val;
            int val2 = head2 == NULL ? 0 : head2->val;
            //相加
            int temp = val1 + val2 + carry;
            //获取进位
            carry = temp / 10;
            temp %= 10;
            //添加元素
            head->next = new ListNode(temp);
            head = head->next;
            //移动下一个
            if(head1 != NULL)
                head1 = head1->next;    // 确实一个规避段错误的小技巧
            if(head2 != NULL)
                head2 = head2->next;
        }
        //结果反转回来
        return ReverseList(res->next); 
    }
};

Linked list addition (2)

Description Two and one have two more inversions

Assuming that the value of each node in the linked list is between 0 - 9, then the linked list as a whole can represent an integer.

Given two such linked lists, generate the resulting linked list representing the value of the addition of the two integers.

class Solution {
public:
    //反转链表
    ListNode* ReverseList(ListNode* pHead) { 
        if(pHead == NULL)
            return NULL;
        ListNode* cur = pHead;
        ListNode* pre = NULL;
        while(cur != NULL){
            //断开链表,要记录后续一个
            ListNode* temp = cur->next; 
            //当前的next指向前一个
            cur->next = pre; 
            //前一个更新为当前
            pre = cur; 
            //当前更新为刚刚记录的后一个
            cur = temp; 
        }
        return pre;
    }
    
    ListNode* addInList(ListNode* head1, ListNode* head2) {
        //任意一个链表为空,返回另一个
        if(head1 == NULL) 
            return head2;
        if(head2 == NULL)
            return head1;
        //反转两个链表
        head1 = ReverseList(head1); 
        head2 = ReverseList(head2);
        //添加表头
        ListNode* res = new ListNode(-1); 
        ListNode* head = res;
        //进位符号
        int carry = 0; 
        //只要某个链表还有或者进位还有
        while(head1 != NULL || head2 != NULL || carry != 0){ 
            //链表不为空则取其值
            int val1 = head1 == NULL ? 0 : head1->val; 
            int val2 = head2 == NULL ? 0 : head2->val;
            //相加
            int temp = val1 + val2 + carry; 
            //获取进位
            carry = temp / 10; 
            temp %= 10; 
            //添加元素
            head->next = new ListNode(temp); 
            head = head->next;
            //移动下一个
            if(head1 != NULL) 
                head1 = head1->next;
            if(head2 != NULL)
                head2 = head2->next;
        }
        //结果反转回来
        return ReverseList(res->next); 
    }
};

 

Guess you like

Origin blog.csdn.net/weixin_66151870/article/details/129105959