五、基础算法精讲:链表-删除系列

目录

  • 1、删除链表中的节点
  • 2、删除链表的倒数第 N 个结点
  • 3、删除排序链表中的重复元素
  • 4、删除排序链表中的重复元素 II

1、删除链表中的节点

Leetcode 237

class Solution:
    def deleteNode(self, node):
        node.val = node.next.val
        node.next = node.next.next
class Solution {
public:
    void deleteNode(ListNode* node) {
        node->val = node->next->val;
        node->next = node->next->next;
    }
};
  • 时间复杂度: O ( 1 ) O(1) O(1)
  • 空间复杂度: O ( 1 ) O(1) O(1)

2、删除链表的倒数第 N 个结点

Leetcode 19

class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        left = right = dummy = ListNode(next=head)
        for _ in range(n):
            right = right.next
        while right.next:
            left = left.next
            right = right.next
        left.next = left.next.next
        return dummy.next
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *dummy = new ListNode(0, head);
        ListNode *left = dummy, *right =  dummy;
        while (n -- ) right = right->next;
        while (right->next) left = left->next, right = right->next;
        left->next = left->next->next;
        return dummy->next;
    }
};
  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( 1 ) O(1) O(1)

3、删除排序链表中的重复元素

Leetcode 83

class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if head is None: return None
        cur = head
        while cur.next:
            if cur.next.val == cur.val:
                cur.next = cur.next.next
            else:
                cur = cur.next
        return head
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if (head == nullptr) return nullptr;
        ListNode *cur = head;
        while (cur->next) {
            if (cur->next->val == cur->val) 
                cur->next = cur->next->next;
            else cur = cur->next;
        }
        return head;
    }
};
  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( 1 ) O(1) O(1)

4、删除排序链表中的重复元素 II

Leetcode 82

class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        cur = dummy = ListNode(next=head)
        while cur.next and cur.next.next:
            val = cur.next.val
            if cur.next.next.val == val:
                while cur.next and cur.next.val == val:
                    cur.next = cur.next.next
            else:
                cur = cur.next
        return dummy.next
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode *dummy = new ListNode(0, head), *cur = dummy;
        while (cur->next && cur->next->next) {
            int val = cur->next->val;
            if (cur->next->next->val == val) 
                while (cur->next && cur->next->val == val)
                    cur->next = cur->next->next;
            else cur = cur->next;
        }
        return dummy->next;
    }
};
  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( 1 ) O(1) O(1)

你可能感兴趣的:(算法笔记,2,算法,链表,数据结构)