day2 链表

203. 移除链表元素

/**
 * 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) {
        //删除头结点
        while(head!=nullptr&&head->val==val){
            ListNode* tmp=head;
            head=head->next;
            delete tmp;
        }
        //删除非头结点
        ListNode* cur=head;
        while(cur!=nullptr&&cur->next!=nullptr){//该链表不为空,并且至少有两个节点
            if(cur->next->val==val){
                ListNode* tmp=cur->next;
                cur->next=cur->next->next;
                delete tmp;
            }
            else{
                cur=cur->next;
            }

        }
        return head;
    }
};

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

707. 设计链表

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

/**
 * 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. 反转链表

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

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

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

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

/**
 * 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);
        dummyhead->next=head;
        ListNode* cur=dummyhead;
        int size=0;
        while(cur!=nullptr){
            size++;
            cur=cur->next;
        }
        size=size-1-n;
        cur=dummyhead;
        while(size--){
            cur=cur->next;
        }
        ListNode* tmp=cur->next->next;
        ListNode* tmp1=cur->next;
        cur->next=tmp;
        delete tmp1;
        ListNode* res=dummyhead->next;
        delete dummyhead;
        return res;
    }
};

面试题 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* cura=headA;
        ListNode* curb=headB;
        int sizea=0,sizeb=0;
        int chazhi;
        while(cura!=NULL){
            sizea++;
            cura=cura->next;
        }
        while(curb!=NULL){
            sizeb++;
            curb=curb->next;
        }
        cura=headA;
        curb=headB;
        if(sizea>sizeb){
            chazhi=sizea-sizeb;
             while(chazhi--){
                cura=cura->next;
            }
        }
        else {
            chazhi=sizeb-sizea;
             while(chazhi--){
                curb=curb->next;
            }
        }
        while(cura!=NULL){
            if(cura==curb)return cura;
            cura=cura->next;
            curb=curb->next;
        }
        return NULL;
    }
};

142. 环形链表 II

/**
 * 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;
        ListNode* slow=head;
        while(fast!=NULL&&fast->next!=NULL){
            slow=slow->next;
            fast=fast->next->next;
            if(fast==slow){
                ListNode* index1=fast;
                ListNode* index2=head;
                while(index1!=index2){
                    index1=index1->next;
                    index2=index2->next;
                }
                return index2;
            }
        }
        return NULL;
    }
};

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值