春节刷题day9:[LeetCode:237、1290、876、面试题 02.07]

春节刷题day9:LeetCode

237. 删除链表中的节点

1290. 二进制链表转整数

剑指 Offer 22. 链表中倒数第k个节点

剑指 Offer 06. 从尾到头打印链表

剑指 Offer 24. 反转链表

876. 链表的中间结点

面试题 02.07. 链表相交


1、237. 删除链表中的节点

/**
 * 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;
    }
};

2、1290. 二进制链表转整数

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    int getDecimalValue(ListNode* head) {
    
    
        ListNode* HEAD = new ListNode(1);
        while(head){
    
    
            ListNode* r = head -> next;
            head -> next = HEAD -> next;
            HEAD -> next = head;
            head = r;
        }
        HEAD = HEAD -> next;
        int ans = 0, k = 0;
        while(HEAD){
    
    
            ans += (HEAD -> val) * (1 << k++);
            HEAD = HEAD -> next;
        }
        return ans;
    }
};

3、剑指 Offer 22. 链表中倒数第k个节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    ListNode* getKthFromEnd(ListNode* head, int k) {
    
    
        ListNode* l = head;
        ListNode* r = head;
        int cnt = 1;
        while(r -> next && cnt != k){
    
     r = r -> next; cnt++; }
        while(r -> next){
    
     r = r -> next; l = l -> next; }
        return l;
    }
};

4、剑指 Offer 06. 从尾到头打印链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    vector<int> reversePrint(ListNode* head) {
    
    
        vector<int> ans;
        if(head == NULL) return ans;
        ListNode* HEAD = new ListNode(1);
        while(head){
    
    
            ListNode* r = head -> next;
            head -> next = HEAD -> next;
            HEAD -> next = head;
            head = r;
        }
        HEAD = HEAD -> next;
        while(HEAD){
    
    
            ans.push_back(HEAD -> val);
            HEAD = HEAD -> next;
        }
        return ans;
    }
};
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    vector<int> reversePrint(ListNode* head) {
    
    
        vector<int> ans;
        stack<int> s;
        while(head){
    
    
            s.push(head -> val);
            head = head -> next;
        }
        while(!s.empty()){
    
    
            ans.push_back(s.top());
            s.pop();
        }
        return ans;
    }
};

5、剑指 Offer 24. 反转链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    ListNode* reverseList(ListNode* head) {
    
    
        ListNode* HEAD = new ListNode(1);
        while(head){
    
    
            ListNode* r = head -> next;
            head -> next = HEAD -> next;
            HEAD -> next = head;
            head = r;
        }
        return HEAD -> next;
    }
};

6、876. 链表的中间结点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    ListNode* middleNode(ListNode* head) {
    
    
        ListNode* ans;
        ListNode* HEAD = new ListNode(1);
        HEAD -> next = head;
        ListNode* l = HEAD;
        ListNode* r = HEAD;
        while(r){
    
    
            r = r -> next;
            if(r -> next == NULL){
    
    
                l = l -> next;
                ans = l; break;
            }else{
    
    
                r = r -> next; l = l -> next;
            }
            if(r -> next == NULL){
    
    
                l = l -> next; ans = l; break;
            }
        }
        return ans;
    }
};

7、面试题 02.07. 链表相交

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
    
    
        ListNode* p1 = headA;
        ListNode* p2 = headB;
        while(p1 != p2){
    
    
            p1 = p1 == NULL ? headB : p1 -> next;
            p2 = p2 == NULL ? headA : p2 -> next;
        }
        return p1;
    }
};

2021/2/14完结(今天还在刷题的小伙伴一定很孤独吧,但是越努力越幸运。祝各位小伙伴情人节快乐)。

猜你喜欢

转载自blog.csdn.net/shangzhengyu/article/details/113806153
今日推荐