Leetcode题解——删除链表中的元素

目录

 

83. 删除排序链表中的重复元素   

 82. 删除排序链表中的重复元素 II

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

237. 删除链表中的节点 

203. 移除链表元素

 


83. 删除排序链表中的重复元素   

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        node = head
        while node and node.next:
            if node.val == node.next.val:
                node.next = node.next.next
            else:
                node = node.next
        return head

 82. 删除排序链表中的重复元素 II

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        dummyHead = ListNode(0)
        dummyHead.next = head
        pre, cur = dummyHead, head
        while cur:  #当前节点存在
            while cur.next and cur.val == cur.next.val: #下一个节点存在,且与当前节点值重复
                cur = cur.next  #当前节点后移
            if pre.next == cur: #前一个节点的后节点为当前节点,意味着当前节点未移动,且后一个节点不重复
                pre = pre.next  #前一个节点后移
            else:               #前一个节点的后节点不为当前节点,意味着当前节点移动,且后一个节点重复
                pre.next = cur.next #跳过重复部分
            cur = cur.next      #当前节点后移
        return dummyHead.next

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

快慢指针,同剑指offer第14题

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        Node = ListNode(None)
        Node.next = head

        fast, slow = Node, Node

        for i in range(n):
            fast = fast.next

        while fast.next != None:
            fast = fast.next
            slow = slow.next

        slow.next = slow.next.next

        return Node.next

237. 删除链表中的节点 

  1. 将本节点的值改为下一个节点的值
  2. 本节点的下一个节点改为下下一个节点
/** c++代码
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
        node->val = node->next->val;
        node->next = node->next->next;
    }
};
###Python代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteNode(self, node):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        node.val = node.next.val
        node.next = node.next.next

203. 移除链表元素

 

 

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        sentinel = ListNode(0)
        sentinel.next = head # 设置哨兵节点

        prev, cur = sentinel, head # 前继节点,当前节点

        while cur: # 循环终止条件
            if cur.val == val:
                prev.next = cur.next  # 删除节点
            else:
                prev = cur # 前继指针滑动
            cur = cur.next # 当前指针滑动
        
        return sentinel.next
class Solution {
  public:
  ListNode* removeElements(ListNode* head, int val) {
    ListNode* sentinel = new ListNode(0);
    sentinel->next = head;

    ListNode *prev = sentinel, *curr = head, *toDelete = nullptr;
    while (curr != nullptr) {
      if (curr->val == val) {
        prev->next = curr->next;
        toDelete = curr;
      } else prev = curr;

      curr = curr->next;

      if (toDelete != nullptr) {
        delete toDelete;
        toDelete = nullptr;
      }
    }

    ListNode *ret = sentinel->next;
    delete sentinel;
    return ret;
  }
};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值