代码随想录刷题第3天|LeetCode203移除链表元素、LeetCode707设计链表、LeetCode206反转链表

1、LeetCode203 移除链表元素

题目链接:203、移除链表元素

首先定义虚拟头结点 ListNode * dummyHead = new ListNode(0);  dummyHead->next = head;

用一个中间变量在链表中移动,首先将它指向虚拟头结点 ListNode * cur = dummyHead;

当cur->next是要删除的元素时,cur->next = cur->next->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* removeElements(ListNode* head, int val) {
        ListNode * dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode * cur = dummyHead;

        while (cur->next != NULL)
        {
            if (cur->next->val == val)
            {
                ListNode *temp = cur->next;
                cur->next = cur->next->next;
                delete temp;
            }
            else
            {
                cur = cur->next;
            }

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

    }
};

2、LeetCode707 设计链表

题目链接:707、设计链表

class MyLinkedList {
public:
    struct LinkNode
    {
        int val;
        LinkNode * next;
        LinkNode(int val):val(val),next(NULL){}
    };

    MyLinkedList() {
        _dummyHead = new LinkNode(0);
        _size = 0;
    }
    
    int get(int index) {
        if (index > (_size-1) || index < 0)
        {
            return -1;
        }
        LinkNode * cur = _dummyHead->next;
        while(index--)
        {
            cur = cur->next;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        LinkNode * newNode = new LinkNode(val);
        newNode->next = _dummyHead->next;
        _dummyHead->next = newNode;
        _size++;
    }
    
    void addAtTail(int val) {
        LinkNode * newNode = new LinkNode(val);
        LinkNode * cur = _dummyHead;
        while(cur->next!=NULL)
        {
            cur = cur->next;
        }
        cur->next = newNode;
        _size++;
    }
    
    void addAtIndex(int index, int val) {
        if (index > _size )
        {
            return ;
        }
        if (index < 0)
        {
            index = 0;
        }
        LinkNode * newNode = new LinkNode(val);
        LinkNode * 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
        {
            LinkNode * cur = _dummyHead;
            while(index--)
            {
                cur = cur->next;
            }
            LinkNode * temp = cur->next;
            cur->next = cur->next->next;
            delete temp;
            _size--;
        }
    }

private:
    int _size;
    LinkNode * _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);
 */

3、反转链表

题目链接:206、反转链表

思想:双指针,cur指向头结点,pre为NULL;在遍历时,temp指向cur->next,cur->next = pre,pre = cur,cur = temp;

/**
 * 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 * temp;
        ListNode * cur = head;
        ListNode * pre = NULL;
        while (cur)
        {
            temp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = temp;
        }
        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 == NULL)
        {
            return pre;
        }
        ListNode * temp = cur->next;
        cur->next = pre;
        return reverse(cur, temp);

    }
    ListNode* reverseList(ListNode* head) {
        return reverse(NULL,head);
    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值