代码随想录算法训练营第三天| LeetCode203.移除链表元素,LeetCode707.设计链表,LeetCode206.反转链表

题目链接:203. 移除链表元素 - 力扣(LeetCode)

文章讲解:代码随想录 (programmercarl.com)

视频讲解:手把手带你学会操作链表 | LeetCode:203.移除链表元素_哔哩哔哩_bilibili

思路:

虚拟头结点

关键点:待删除结点,先用tmp指针另存,然后手动释放

// C++代码

/**
 * 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* dummy_head = new ListNode(0);
        dummy_head->next = head;
        ListNode* cur = dummy_head;

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

        head = dummy_head->next;
        delete dummy_head;
        return head;
    }
};
# python代码

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        dummy_head = ListNode(next=head)

        cur = dummy_head
        while cur.next :
            if cur.next.val == val :
                cur.next = cur.next.next
            else :
                cur = cur.next
        
        return dummy_head.next
        

题目链接:707. 设计链表 - 力扣(LeetCode)

文章讲解:​​​​​​​代码随想录 (programmercarl.com)

视频讲解:​​​​​​​​​​​​​​帮你把链表操作学个通透!LeetCode:707.设计链表_哔哩哔哩_bilibili

链表的基本操作:

关键点:插入时先考虑后面的节点,在考虑前面的节点;

// C++代码

class MyLinkedList {
public:
    struct LinkedNode {
        int val;
        LinkedNode* next;
        LinkedNode() : val(0), next(nullptr) {}
        LinkedNode(int x) : val(x), next(nullptr) {}
        LinkedNode(int x, LinkedNode* next) : val(x), next(next) {}
    };

    MyLinkedList() {
        _dummy_head = new LinkedNode(0);
        _size = 0;
    }
    
    int get(int index) {
        if (index > (_size - 1) || index < 0) return -1;

        LinkedNode* cur = _dummy_head;
        while (index--) {
            cur = cur->next;
        }
        return cur->next->val;
    }
    
    void addAtHead(int val) {
        LinkedNode* new_node = new LinkedNode(val);
        new_node->next = _dummy_head->next;
        _dummy_head->next = new_node;
        _size++;
    }
    
    void addAtTail(int val) {
        LinkedNode* new_node = new LinkedNode(val);
        LinkedNode* cur = _dummy_head;
        while (cur->next != nullptr) {
            cur = cur->next;
        } 
        new_node->next = cur->next;
        cur->next = new_node;
        _size++;
    }
    
    void addAtIndex(int index, int val) {
        if (index > _size) return;
        if (index < 0) index = 0;

        LinkedNode* new_node = new LinkedNode(val);
        LinkedNode* cur = _dummy_head;
        while (index--) {
            cur = cur->next;
        }
        new_node->next = cur->next;
        cur->next = new_node;
        _size++;
    }
    
    void deleteAtIndex(int index) {
        if (index < 0 || index > (_size - 1)) return;

        LinkedNode* cur = _dummy_head;
        while (index--) {
            cur = cur->next;
        }
        LinkedNode* tmp = cur->next;
        cur->next = cur->next->next;
        delete tmp;
        tmp = nullptr;
        _size--;
    }

    // void printLinkedList() {
    //     LinkedNode* cur = _dummy_head;
    //     while (cur->next != nullptr) {
    //         cout << cur->next->val << " ";
    //         cur = cur->next;
    //     }
    //     cout << endl;
    //     cout << get(0) << endl;
    //     cout << get(1) << endl;
    //     cout << get(2) << endl;
    // }

private:
    LinkedNode* _dummy_head;
    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. 反转链表 - 力扣(LeetCode)

文章讲解:​​​​​​​​​​​​​​​​​​​​​代码随想录 (programmercarl.com)

视频讲解:​​​​​​​​​​​​​​​​​​​​​帮你拿下反转链表 | LeetCode:206.反转链表 | 双指针法 | 递归法_哔哩哔哩_bilibili

双指针法

关键点:注意tmp

// C++代码

/**
 * 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* pre = NULL;
        ListNode* cur = head;
        ListNode* tmp = NULL;

        while (cur) {
            tmp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = tmp;
        }

        return pre;
    }
};
#python

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        pre = None
        cur = head
        tmp = None

        while cur :
            tmp = cur.next
            cur.next = pre
            pre = cur
            cur = tmp
        
        return pre

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值