LeetCode Algorithm C++ Brush Questions-addTwonumbers

Today's question

Two linked lists in reverse order are required to be added from the low order, and the result is also output in reverse order, and the return value is the head node of the reversed result linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

Problem-solving ideas:

Need to pay attention to various carry situations. For example in special cases:
Input: (9 -> 9 -> 9 -> 9 -> 9) + (1 -> )
Output: 0 -> 0 -> 0 -> 0 -> 0 -> 1
In order to unify the processing method , you can create a virtual head node first, and the Next of this virtual head node points to the real head, so that the
head does not need to be processed separately, and the while loop can be used directly. In addition, the condition for judging the termination of the loop does not need to be p.Next! = nil, so the last
bit needs to be calculated additionally, and the loop termination condition should be p != nil.

Sample code:

class addTwoNumbers{
    
    
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    
    
		ListNode* re = new ListNode(0);
        ListNode* r = re;
        int carry = 0;
        while(l1!=NULL || l2!=NULL)
        {
    
    
            int x = 0;
            int y = 0;
            if(l1!=NULL)
            {
    
    
                x = l1->val;    
            }else{
    
    
                x = 0;
            }
            if(l2!=NULL)
            {
    
    
                y = l2->val;      
            }else{
    
    
                y = 0;
            }
            int s = carry + x + y;
            carry = s/10;
            r->next = new ListNode(s%10);
            r = r->next;
            if(l1!=NULL)
            {
    
    
                l1 = l1->next;
            }
            if(l2!=NULL)
            {
    
    
                l2 = l2->next;
            }        
        }
        if(carry > 0){
    
    
           r->next = new ListNode(carry%10);
        }
        return re->next;
    }
};

Preliminary knowledge: linked list.

1. Linked list image explanation

Imagine: there is a little note with the address of a drawer with something you need in that drawer and a new little note with the address, which points to a new drawer.

Linked List Diagram

2. Linked list c++ demo
1. The basic data structure of linked list
struct ListNode
{
    
    
    double value;
    ListNode *next;
};

It can be seen that the ListNode structure has an interesting property, which contains a pointer to the same type of data structure
(1) The value stored in a single node can be a structure, or the data type you want,
(2) You can also add pointer to previous node

struct data
{
    
    
   int number;
   string name;
   string sex;
};
struct ListNode
{
    
    
    struct data *info;
    ListNode *next;
    ListNode *last;
};
2. Create a linked list (single linked list)

(1) Create the head node head, and point the current node p to the head node (p=head)
(2) Create the next node q, and the next node of the current node p is q (p->next= q)
(3) Move the node p backward by one bit (p = p->next)

#include <iostream>
#include<vector>

using namespace std;

struct ListNode{
    
    
	int val;
	struct ListNode *next;
	ListNode(int x) :
		val(x), next(NULL){
    
    
	}
};

int main()
{
    
    
	int num;
	cin >> num;
	ListNode* head = new ListNode(num);
	ListNode* p = head;
	
	//利用尾插法创建一个链表
	while (cin >> num){
    
    
		ListNode* q = new ListNode(num);
		p->next = q; 
		p = p->next;
	}

	//遍历这个链表,并输出每个结点的元素
	ListNode* m = head;
	while (m != nullptr){
    
    
		cout << m->val << endl;
		m = m->next;
	}

	return 0;
}
3. Insert node

1. Determine whether the original linked list is an empty linked list, if so, point the head to the newly added node
2. If it is not an empty linked list, insert a new node at the end of the linked list

ListNode* insertNode(ListNode* head, int data){
    
    
	ListNode* newNode = new ListNode(data);
	ListNode* p = head;
	if (p == nullptr){
    
    
		head = newNode;
	}
	else{
    
    
		while (p->next != nullptr){
    
    
			p = p->next;
		}
		p->next = newNode;
	}
	return head;
}
4. Delete node

diagram
Basic idea: If there is a node to be deleted in the linked list, traverse to the previous node of the node to be deleted, and point the next pointer of the node to the next node of the node to be deleted.

ListNode* deleteNode(ListNode* head, int data){
    
    
	ListNode* p = head;
	//首先判断是不是空链表
	if (p == nullptr){
    
    
		return head;
	}
	else{
    
    
		//判断是不是删除头节点
		if (p->val == data){
    
    
			head = p->next;
			delete p;
			return head;
		}
		else{
    
    
			//如果有该结点,遍历到待删除节点的前一节点
			while (p->next != nullptr && p->next->val != data){
    
    
				p = p->next;
			}
			//遍历完整个链表都没有待删除节点
			if (p->next == nullptr){
    
    
				return head;
			}
			else{
    
    
				ListNode* deleteNode = p->next;
				p->next = deleteNode->next;
				delete deleteNode;
				return head;
			}
		}
	}
}
reference:

https://blog.csdn.net/slandarer/article/details/91863177

Guess you like

Origin blog.csdn.net/u010196944/article/details/127570932