Leetcode 83 删除排序链表中的重复元素【链表】本题是简单的链表题。

1.思路
1)单指针,下一个和上一个相同,p.next指向p.next.next,否则p指针后移

这里不用哑节点,直接开始就判断

2.代码

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

class Solution:
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head == None or head.next == None:
            return head
        
        p = head
        while p.next:
            if p.val == p.next.val:
                p.next = p.next.next
            else:
                p = p.next            
        return head

相关文章:

  • 2022-12-23
  • 2021-05-31
  • 2021-10-28
  • 2022-01-01
  • 2021-05-01
  • 2022-12-23
  • 2021-09-30
猜你喜欢
  • 2021-12-19
  • 2021-08-20
  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案