7.10 学习记录

目录

203.移除链表元素

707.设计链表

206.反转链表

24. 两两交换链表中的节点

19.删除链表的倒数第N个节点

面试题 02.07. 链表相交

142.环形链表II


203.移除链表元素

203. 移除链表元素 - 力扣(LeetCode)

/**
 * 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* removeElements(ListNode* head, int val) {
        ListNode *dummyhead = new ListNode(0,head);
        ListNode *pre = dummyhead;
        ListNode *cur = head;
        while (cur!= nullptr)
        {
            if (cur->val==val)
            {
                pre->next = cur->next;
                delete cur;
                cur = pre->next;
            }
            else
            {
                cur = cur->next;
                pre = pre->next;
            }
        }
        return dummyhead->next;
    }
};

707.设计链表

707. 设计链表 - 力扣(LeetCode)

class MyLinkedList {
private:
    struct ListNode{
        int val;
        ListNode* next;
        ListNode():val(0),next(nullptr){}
        ListNode(int v):val(v),next(nullptr){}
        ListNode(int v, ListNode* n):val(v),next(n){}
    };
    int tailindex;
    ListNode* dummyhead;
public:
    MyLinkedList() {
        tailindex = -1;
        dummyhead = new ListNode();
    }
    
    int get(int index) {
        if(index<0 || index>tailindex)return -1;
        ListNode *cur = dummyhead->next;
        while(index){
            cur = cur->next;
            index--;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        ListNode* cur =new ListNode(val,dummyhead->next);
        dummyhead->next = cur;
        tailindex++;
    }
    
    void addAtTail(int val) {
        ListNode* cur =new ListNode(val);
        ListNode *pre = dummyhead;
        while (pre->next){
            pre = pre->next;
        }
        pre->next = cur;
        tailindex++;
    }
    
    void addAtIndex(int index, int val) {
        if(index<0 || index>tailindex+1)return;
        if (index==0)addAtHead(val);
        else if(index==tailindex+1)addAtTail(val);
        else{
            ListNode *cur = dummyhead;
            while(index!=0){
                cur = cur->next;
                index--;
            }
            ListNode *n = new ListNode(val,cur->next);
            cur->next = n;
            tailindex++;
        }
    }
    
    void deleteAtIndex(int index) {
        if(index<0 || index>tailindex) return;
        ListNode*cur = dummyhead;
        while (index!=0){
            cur = cur->next;
            index--;
        }
        ListNode *tmp = cur->next;
        cur->next = cur->next->next;
        delete tmp;
        tailindex--;
    }
};

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList* obj = new MyLinkedList();
 * int param_1 = obj->get(index);
 * obj->addAtHead(val);
 * obj->addAtTail(val);
 * obj->addAtIndex(index,val);
 * obj->deleteAtIndex(index);
 */

206.反转链表

206. 反转链表 - 力扣(LeetCode)

/**
 * 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* reverseList(ListNode* head) {
        if (head==nullptr || head->next==nullptr)return head;
        ListNode *pre = nullptr,*cur = head,*tmp = cur->next;
        while(tmp){
            cur->next = pre;
            pre = cur;
            cur = tmp;
            tmp = tmp->next;
        }
        cur->next = pre;
        return cur;
    }
};
/**
 * 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* reverseList(ListNode* head) {
        if (head ==nullptr || head->next==nullptr)return head;
        ListNode *tmp = reverseList(head->next);
        head->next->next = head;
        head->next = nullptr;
        return tmp;    
    }
};

24. 两两交换链表中的节点

24. 两两交换链表中的节点 - 力扣(LeetCode)

/**
 * 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* swapPairs(ListNode* head) {
        if (head ==nullptr || head->next==nullptr)return head;
        ListNode*dummyhead = new ListNode(0,head);
        ListNode *pre = dummyhead,*cur = dummyhead->next;
        while(cur!=nullptr && cur->next!=nullptr){
            ListNode*tmp = cur->next->next;
            pre->next = cur->next;
            pre->next->next = cur;
            cur->next = tmp;
            pre = cur;
            cur = tmp;
        }
        return dummyhead->next;
    }
};
/**
 * 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* swapPairs(ListNode* head) {
        if (head ==nullptr || head->next==nullptr)return head;
        ListNode* tmp =head->next;
        head->next = swapPairs(tmp->next);
        tmp->next= head;
        return tmp;
    }
};

19.删除链表的倒数第N个节点

19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

/**
 * 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* removeNthFromEnd(ListNode* head, int n) {
        ListNode *dummyhead = new ListNode(0,head);
        ListNode *fast = dummyhead,*slow = dummyhead;
        while(n!=-1){
            fast = fast->next;
            n--;
        }
        while (fast!=nullptr){
            fast =fast->next;
            slow = slow->next;
        }
        ListNode*tmp = slow->next;
        slow->next = slow->next->next;
        delete tmp;
        return dummyhead->next;
    }
};

面试题 02.07. 链表相交

160. 相交链表 - 力扣(LeetCode)

/**
 * 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,*p2 = headB;
        while(p1!=p2){
            if(p1!=nullptr){
                p1 = p1->next;
            }else p1 = headB;
            if (p2!=nullptr){
                p2 = p2->next;
            }else p2 = headA;
        }
        return p1;
    }
};

142.环形链表II

142. 环形链表 II - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *fast = head,*slow = head;
        while (fast!=nullptr && fast->next!=nullptr){
            fast = fast->next->next;
            slow = slow->next;
            if(fast ==slow){
                fast = head;
                while (fast!=slow){
                    fast = fast->next;
                    slow = slow->next;
                }
                return fast;
            }
        }
        return nullptr;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值