82. Remove Duplicates from Sorted List II(Python3)
2018-01-29 17:43
477 查看
82. Remove Duplicates from Sorted List II(Python3)
题目
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
解题方案
思路:该题并不复杂,主要是指针的操作,其中要注意的是,最后一个元素如果有多个相同的要进行剔除,这里的处理
代码:
# 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 not head: return head tmp = ListNode(0) tmp.next = head pre = tmp front = head end = head.next flag = False while end: if front.val == end.val: end = end.next flag = True elif flag: pre.next,front,end = end,end,end.next flag = False else: pre,front,end = front,end,end.next if flag: pre.next = None return tmp.next
相关文章推荐
- 【LEETCODE】82- Remove Duplicates from Sorted List II [Python]
- [LeetCode82]Remove Duplicates from Sorted List II
- leetcode-Remove Duplicates from Sorted List II-82
- [LeetCode]82 Remove Duplicates from Sorted List II
- 【Leetcode】【python】Remove Duplicates from Sorted List/Remove Duplicates from Sorted List II
- LeeCode 82 Remove Duplicates from Sorted List II
- leetcode_82_Remove Duplicates from Sorted List II
- leetcode 82-- Remove Duplicates from Sorted List II 链表 双指针 删除元素
- LeetCode---(82) Remove Duplicates from Sorted List II
- 82-Remove Duplicates from sorted list II
- LeetCode 82 Remove Duplicates from Sorted List II
- FTPrep, 82 Remove Duplicates from Sorted List II
- Leetcode 82 Remove Duplicates from Sorted List II
- LeetCode-82-Remove Duplicates from Sorted List II(链表)-Medium
- 【leetcode c++】82 Remove Duplicates from Sorted List II
- LeetCode-82-Remove Duplicates from Sorted List II 链表
- [C语言][LeetCode][82]Remove Duplicates from Sorted List II
- LeetCode82:Remove Duplicates from Sorted List II
- 【LeetCode】82 - Remove Duplicates from Sorted List II
- Leetcode-82: Remove Duplicates from Sorted List II