第二章 链表(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) {}
     };

203.移除链表元素

题目链接

  • 使用头节点,统一原头节点和其他节点的删除操作。
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* dummyHead = new ListNode(0, head);
        ListNode* pre = dummyHead;

        while (pre->next != nullptr) {
            if (pre->next->val == val) {
                ListNode* tmp = pre->next;
                pre->next = pre->next->next;
                delete tmp;
            } else {
                pre = pre->next;
            }
        }
        
        head = dummyHead->next;
        delete dummyHead;
        return head;
    }
};

707.设计链表

题目链接

  • 使用虚拟头节点。注意插入删除区间选择,以及长度维护。
class MyLinkedList {
public:
    struct LinkedNode {
        int val;
        LinkedNode* next;

        // LinkedNode():val(0),next(nullptr){}
        LinkedNode(int v) : val(v), next(nullptr) {}
        // LinkedNode(int v,LinkedNode n):val(v),next(n){}
    };

    MyLinkedList() {
        _dummyNode = new LinkedNode(0);
        _size = 0;
    }

    int get(int index) {
        if (index < 0 || index >= _size)
            return -1;
        LinkedNode* pre = _dummyNode;
        while (index--) {
            pre = pre->next;
        }
        return pre->next->val;
    }

    void addAtHead(int val) {
        LinkedNode* head = new LinkedNode(val);
        head->next = _dummyNode->next;
        _dummyNode->next = head;
        ++_size;
    }

    void addAtTail(int val) {
        LinkedNode* tail = new LinkedNode(val);
        LinkedNode* pre = _dummyNode;
        while (pre->next != nullptr) {
            pre = pre->next;
        }
        pre->next = tail;
        ++_size;
    }

    void addAtIndex(int index, int val) {
        if (index < 0 || index > _size)
            return;
        LinkedNode* in = new LinkedNode(val);
        LinkedNode* pre = _dummyNode;
        while (index--) {
            pre = pre->next;
        }
        in->next = pre->next;
        pre->next = in;
        ++_size;
    }

    void deleteAtIndex(int index) {
        if (index < 0 || index >= _size)
            return;
        LinkedNode* pre = _dummyNode;
        while (index--) {
            pre = pre->next;
        }
        LinkedNode* tmp = pre->next;
        pre->next = pre->next->next;
        delete tmp;
        --_size;
    }

private:
    LinkedNode* _dummyNode;
    int _size;
};

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

题目链接

  • 理解临时指针tmp的使用,tmp指向脱离的部分tmp = cur->next
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* pre = nullptr;
        ListNode* cur = head;

        while (cur != nullptr) {
            ListNode* tmp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = tmp;
        }

        return pre;
    }
};

24.两两交换链表中的元素

题目链接

  • 与206.反转链表相比,多一个指针,主要在与如何维护这三个指针
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (head == nullptr || head->next == nullptr)
            return head;

        ListNode* dummyHead = new ListNode(0, head);
        ListNode* pre = dummyHead;
        ListNode* cur = head;
        ListNode* nex = head->next;

        while (cur != nullptr && cur->next != nullptr) {
            ListNode* tmp = nex->next;
            pre->next = nex;
            cur->next = tmp;
            nex->next = cur;

            pre = cur;
            cur = tmp;
            if (cur != nullptr)
                nex = cur->next;
        }

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

19.删除链表的倒数第N个元素

题目链接

  • 双指针法,让右指针走到最后一个元素,左指针与它相差N-1位即可,需要删除节点使用preLe指向待删除节点的前一节点。
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        int diff = n - 1;
        ListNode* dummyHead = new ListNode(0, head);
        ListNode* preLe = dummyHead;//待删除节点的前一节点
        ListNode* ri = head;

        while (diff--) {
            ri = ri->next;
        }

        while (ri->next != nullptr) {
            ri = ri->next;
            preLe = preLe->next;
        }

        ListNode* tmp = preLe->next;
        preLe->next = preLe->next->next;
        delete tmp;

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

160.链表相交

题目链接

  • 使用双指针,先遍历一遍得出各自长度,再置于同一起跑线移动。
class Solution {
public:
    ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
        ListNode* ptrA = headA;
        ListNode* ptrB = headB;
        int lenA = 0;
        int lenB = 0;

        while (ptrA != nullptr) {
            ptrA = ptrA->next;
            lenA++;
        }
        while (ptrB != nullptr) {
            ptrB = ptrB->next;
            lenB++;
        }

        ptrA = headA;
        ptrB = headB;
        if (lenB > lenA) {
            swap(ptrA, ptrB);
            swap(lenA, lenB);
        }

        int diff = lenA - lenB;
        while (diff--) {
            ptrA = ptrA->next;
        }
        while (ptrA != nullptr) {
            if (ptrA == ptrB)
                return ptrA;
            ptrA = ptrA->next;
            ptrB = ptrB->next;
        }

        return nullptr;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值