代码随想录 | 链表总结

203.移除链表元素

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* newnode = new ListNode(0);
        newnode->next = head;
        
        ListNode* cur =newnode;
        
        while(cur->next!=nullptr)
        {
            if(cur->next->val == val)
            {
                ListNode* temp = cur->next;
                cur->next  = temp->next;
                delete temp;
            }else{
                cur = cur->next;
            }
        }

        head =newnode->next;
        delete newnode;
        return head;
    }
};

707.设计链表

class MyLinkedList {
public:
    struct LinkedNode {
        int val;
        LinkedNode* next;
        LinkedNode(int x) {
            this->val = x;
            this->next = nullptr;
        }
    };

    LinkedNode* head;

    MyLinkedList() : head(nullptr) {}

    int get(int index) {

        if (head == nullptr) {
            return -1;
        }

        if (index < 0) {
            return -1;
        }

        LinkedNode* cur = head;

        if (index == 0) {
            return cur->val;
        }

        for (int i = 0; i <index &&cur!=nullptr; i++) {
            cur = cur->next;
        }

        if(cur==nullptr)
        {
            return-1;
        }


        return cur->val;
    }

    void addAtHead(int val) {
        LinkedNode* node = new LinkedNode(val);

        if (head == nullptr) {
            node->next = head;
            head = node;
            return;
        }

        node->next = head;
        head = node;
        return;
    }

    void addAtTail(int val) {
        LinkedNode* node = new LinkedNode(val);

        if (head == nullptr) {
            head = node;
            return;
        }

        LinkedNode* cur = head;

        while (cur->next != nullptr) {
            cur = cur->next;
        }
        cur->next = node;
        return;
    }

    void addAtIndex(int index, int val) {
        LinkedNode* cur = head;
        LinkedNode* node = new LinkedNode(val);

        if (index == 0) {
            node->next = head;
            head = node;
            return;
        }

        for (int i = 0; i < index - 1 && cur != nullptr; i++) {
            cur = cur->next;
        }

        if (cur == nullptr) {
            return;
        }

        if (cur->next == nullptr) {
            cur->next = node;
            return;
        }

        node->next = cur->next;
        cur->next = node;
        return;
    }

    void deleteAtIndex(int index) {
        LinkedNode* cur = head;

        if (index == 0) {
            head = cur->next;
            delete cur;
            return;
        }
        for (int i = 0; i < index - 1 && cur != nullptr; i++) {
            cur = cur->next;
        }

        if (cur == nullptr || cur->next == nullptr) {
            return;
        }

        LinkedNode* temp = cur->next;
        cur->next = temp->next;
        delete temp;
        return;
    }

    // void print() {
    //     LinkedNode* cur = head;

    //     while (cur != nullptr) {
    //         cout << cur->val << "->";
    //         cur = cur->next;
    //     }
    //     cout << endl;
    // }

    ~MyLinkedList() {
        LinkedNode* cur = head;
        while (cur != nullptr) {
            LinkedNode* temp = cur;
            cur = cur->next;
            delete temp;
        }
    }
};

206.反转链表

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* cur =head;
        ListNode* pre =nullptr;
        while(cur!=nullptr)
        {
            ListNode* temp =cur->next;
            cur->next = pre;
            pre = cur;
            cur =temp;
        }
        
        return pre;
    }
};

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

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
       ListNode* dummyHead = new ListNode(0);
       dummyHead->next =head;
       
       ListNode* cur =dummyHead;
       while(cur->next!=nullptr && cur->next->next!=nullptr)
       {
            ListNode* temp1 = cur->next;
            ListNode* temp2 =cur->next->next->next;

            cur->next = cur->next->next;
            cur->next->next =temp1;
            cur->next->next->next =temp2;

            cur = cur->next->next;
       }

       cur = dummyHead->next;
       delete dummyHead;
       return cur;
    }
};

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

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        // ListNode* cur = head;
        // int num = 0;
        // while (cur != nullptr) {
        //     num++;
        //     cur = cur->next;
        // }

        // cur = head;

        // if (num < n) {
        //     return cur;
        // }

        // if (num == n) {
        //     cur = head->next;
        //     delete head;
        //     return cur;
        // }

        // for (int i = 0; i < num - n - 1 && cur != nullptr; i++) {
        //     cur = cur->next;
        // }

        // if (cur->next == nullptr) {
        //     return cur;
        // }

        // ListNode* temp = cur->next;
        // cur->next = temp->next;
        // delete temp;
        // return head;

        
       ListNode* dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode* slow = dummyHead;
        ListNode* fast = dummyHead;
        while(n-- && fast != NULL) {
            fast = fast->next;
        }
        fast = fast->next; // fast再提前走一步,因为需要让slow指向删除节点的上一个节点
        while (fast != NULL) {
            fast = fast->next;
            slow = slow->next;
        }
        slow->next = slow->next->next; 
        
        // ListNode *tmp = slow->next;  C++释放内存的逻辑
        // slow->next = tmp->next;
        // delete tmp;
        
        return dummyHead->next;
    }
};

面试题 02.07. 链表相交

class Solution {
public:
    ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
        ListNode* curA = headA;
        ListNode* curB = headB;

        int lenA = 0;
        while (curA != NULL) {
            lenA++;
            curA = curA->next;
        }

        int lenB = 0;
        while (curB != NULL) {
            lenB++;
            curB = curB->next;
        }

        curA = headA;
        curB = headB;
        if(lenA>lenB)
        {
            for(int i=0;i<lenA-lenB;i++)
            {
                curA =curA->next;
            }
        }
        else{
            for(int i=0;i<lenB-lenA;i++)
            {
                curB =curB->next;
            }
        }

        while(curA!=NULL)
        {
            if(curA == curB)
            {
                return curA;
            }
            curA =curA->next;
            curB =curB->next;
        }

        return NULL;
    }
};

142.环形链表II

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode* fast = head;
        ListNode* slow = head;
        
        while(fast!=NULL && fast->next!=NULL)
        {
            fast =fast->next->next;
            slow = slow->next;
            if(fast==slow)
            {
                ListNode* index1 =fast;
                ListNode* index2 = head;
                while(index1!=index2)
                {
                    index1 = index1->next;
                    index2 = index2->next;
                }
                return index1;
            }
        }
        return NULL;
    }
};

链表结束。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值