[LeetCode]Remove Duplicates from Sorted List@python
2018-01-29 15:37
471 查看
Given a sorted linked list, delete all duplicates such that each element appear only
once.
For example,
Given
Given
思路:没啥难度,注意空链表和长度只有1的链表即可.
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return head
if not head.next:
return head
tmp = head
while tmp.next:
if tmp.val == tmp.next.val:
tmp.next = tmp.next.next
else:
tmp = tmp.next
return head
once.
For example,
Given
1->1->2, return
1->2.
Given
1->1->2->3->3, return
1->2->3.
思路:没啥难度,注意空链表和长度只有1的链表即可.
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return head
if not head.next:
return head
tmp = head
while tmp.next:
if tmp.val == tmp.next.val:
tmp.next = tmp.next.next
else:
tmp = tmp.next
return head
相关文章推荐
- Leetcode-Remove Duplicates from Sorted List-Python
- [LeetCode]Remove Duplicates from Sorted List II@python
- leetcode 【 Remove Duplicates from Sorted List 】 python 实现
- [leetcode]Remove Duplicates from Sorted List II @ Python
- 【LEETCODE】82- Remove Duplicates from Sorted List II [Python]
- [LeetCode]题解(python):082-Remove Duplicates from Sorted List II
- [LeetCode]题解(python):083-Remove Duplicates from Sorted List
- [leetcode]Remove Duplicates from Sorted List @ Python
- Python学习——leetcode(Remove Duplicates from Sorted List)
- LeetCode 83 — Remove Duplicates from Sorted List(C++ Java Python)
- 【Leetcode】【python】Remove Duplicates from Sorted List/Remove Duplicates from Sorted List II
- 【LeetCode with Python】 Remove Duplicates from Sorted List II
- 【LeetCode 83】Remove Duplicates from Sorted List (Python)
- leetcode 【 Remove Duplicates from Sorted List II 】 python 实现
- 链表-python-leetcode 83 Remove Duplicates from Sorted List
- 【LeetCode with Python】 Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List Leetcode Python
- leetcode 38: Remove Duplicates from Sorted List II
- 【leetcode c++】82 Remove Duplicates from Sorted List II
- Leetcode: Remove Duplicates from Sorted List II