Prepare for the second test, three questions a day, Day25

Prepare for the second test, three questions a day

Topic 1: delete nodes in the linked list

Please write a function to delete a specific node in the single linked list. When designing the function, you should pay attention to that you cannot access the head node of the linked list. You can only directly access the node to be deleted.

The topic data ensures that the node to be deleted is not the end node.

Example 1:

Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: specify the second node with a value of 5 in the linked list. After calling your function, the linked list should be 4 - > 1 - > 9
Example 2:

Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Explanation: specify the third node with a value of 1 in the linked list. After calling your function, the linked list should be 4 - > 5 - > 9
Example 3:

Input: head = [1,2,3,4], node = 3
Output: [1,2,4]
Example 4:

Input: head = [0,1], node = 0
Output: [1]
Example 5:

Input: head = [-3,5,-99], node = -3
Output: [5, - 99]

Tips:

The number range of nodes in the linked list is [2, 1000]
-1000 <= Node.val <= 1000
The value of each node in the linked list is unique
The node to be deleted is a valid node in the linked list and is not the end node

Source: LeetCode
Link: https://leetcode-cn.com/problems/delete-node-in-a-linked-list

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
        node->val=node->next->val;
        node->next=node->next->next;
    }
};

Topic 2: ugly number

Give you an integer n, please judge whether n is an ugly number. If yes, return true; Otherwise, false is returned.

Ugly numbers are positive integers that contain only prime factors 2, 3, and / or 5.

Example 1:

Input: n = 6
Output: true
Explanation: 6 = 2 × three
Example 2:

Input: n = 8
Output: true
Explanation: 8 = 2 × two × two
Example 3:

Input: n = 14
Output: false
Explanation: 14 is not an ugly number because it contains another prime factor 7.
Example 4:

Input: n = 1
Output: true
Explanation: 1 is usually regarded as an ugly number.

Tips:

-231 <= n <= 231 - 1

Source: LeetCode
Link: https://leetcode-cn.com/problems/ugly-number

class Solution {
public:
    bool isUgly(int n) {
        int temp=n;
        while(temp>1){
            if(temp%2==0){
                temp/=2;
            }else if(temp%3==0){
                temp/=3;
            }else if(temp%5==0){
                temp/=5;
            }else{
                break;
            }
        }
        if(temp!=1) return false;
        return true;
    }
};

Topic 3: separate linked list

Give you a head node of the linked list and a specific value X. please separate the linked list so that all nodes less than x appear before nodes greater than or equal to X.

You should keep the initial relative position of each node in both partitions.

Example 1:

Input: head = [1,4,3,2,5,2], x = 3
Output: [1,2,2,4,3,5]
Example 2:

Input: head = [2,1], x = 2
Output: [1,2]

Tips:

The number of nodes in the linked list is within the range [0, 200]
-100 <= Node.val <= 100
-200 <= x <= 200

Source: LeetCode
Link: https://leetcode-cn.com/problems/partition-list

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        if(head==nullptr||head->next==nullptr) return head;
        ListNode* dumyNode=new ListNode(0);
        dumyNode->next=head;
        //The nodes greater than or equal to the target value are removed from the original linked list and connected together, and finally spliced to the end of the original linked list
        ListNode* p=dumyNode,*q,*r=nullptr,*l=nullptr;
        while(p->next!=nullptr){
            if(p->next->val<x){
                p=p->next;
            }else{
                q=p->next;
                p->next=p->next->next;
                q->next=nullptr;
                if(r==nullptr){
                    r=q;
                    l=r;
                }else{
                    r->next=q;
                    r=q;
                }
            }
        }
        p->next=l;
        return dumyNode->next;
        
    }
};

Keywords: Algorithm data structure leetcode linked list

Added by aminnuto on Wed, 02 Feb 2022 17:55:45 +0200