Sword Points Offer 11th Day Double Pointer (Simple)

The sword refers to Offer 18. Delete the node of the linked list

Topic description

给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。

返回删除后的链表的头节点。

注意:此题对比原题有改动
示例 1:

输入: head = [4,5,1,9], val = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
示例 2:

输入: head = [4,5,1,9], val = 1
输出: [4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.
说明:
题目保证链表中节点的值互不相同
若使用 C 或 C++ 语言,你不需要 free 或 delete 被删除的节点

Topic ideas and codes

Problem- solving ideas:
In this question, deleting a node with a value of val needs to be divided into two steps: locating the node and modifying the reference.
Locate Node: Traverse the linked list until head.val == val and jump out to locate the target node.
Modify the reference: Set the predecessor node of the node cur as pre, and the successor node as cur.next; then execute pre.next = cur.next to delete the cur node.

class Solution {
    
    
public:
    ListNode* deleteNode(ListNode* head, int val) {
    
    
            if(head->val==val)return head->next;
            ListNode *L=head;
            ListNode *p=head;

            while(L){
    
    
                p=L->next;
                if(p){
    
    
                    if(p->val==val){
    
    
                        if(p->next!=NULL){
    
    
                            p = p->next;
                            L->next=p;
                        }else{
    
    
                            L->next=NULL;
                        }
                    }
                    L=L->next;
                }else{
    
    
                    L = p;
                }
            }
            return head;
    }
};

The sword refers to Offer 22. The kth node from the bottom in the linked list

Topic description

输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。

例如,一个链表有 6 个节点,从头节点开始,它们的值依次是 123456。这个链表的倒数第 3 个节点是值为 4 的节点。

 

示例:

给定一个链表: 1->2->3->4->5, 和 k = 2.

返回链表 4->5.

Topic ideas and codes

Ideas and Algorithms:
The simplest and most direct method is sequential search. Assuming that the length of the current linked list is nn, we know that the last kth node of the linked list is the positive number n - kn−kth node. At this time, we only need The n-kn−kth node of the linked list is traversed sequentially to the last kth node.
We first find the length nn of the linked list, and then traverse to the n - kn−kth node of the linked list in order to return.

class Solution {
    
    
public:
    ListNode* getKthFromEnd(ListNode* head, int k) {
    
    
        int k1=num(head);
        //cout<<k1<<endl;
        int n=0;
        ListNode *L=head,*p=head;
        while(L){
    
    
            n++;
            p=L;
            if(n==(k1-k)){
    
    
                p=L->next;
                return p;
                //L->next=p->next;
            }
            L=L->next;
        }
        return head;
    }
private:
    int num(ListNode* head){
    
    
        int nu=0;
        ListNode *L=head;
        while(L){
    
    
            nu++;
            L=L->next;
        }
        return nu;
    }
};

Recommend to everyone


"If you are undecided, you can ask the spring breeze, and if the spring breeze does not speak, you will follow your heart" means: if you are hesitant about something, ask the spring breeze how to do it. . "When you are undecided, you can ask the spring breeze, and if the spring breeze does not speak, you will follow your heart." The sentence comes from the "Sword Comes" by the online writer "Fenghuo Opera Princes". The original text is: "If you are undecided, you can ask the spring breeze. Follow your heart".

insert image description here


Guess you like

Origin blog.csdn.net/weixin_46627433/article/details/123364792