leetcode 【 Remove Duplicates from Sorted List 】 python 实现
2014-12-26 14:33
267 查看
题目:
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
代码:
思路:
设立虚表头dummyhead
遇上val不同的,再执行 p=p.next;否则指针p不动,只是p.next变化。
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
1->1->2, return
1->2.
Given
1->1->2->3->3, return
1->2->3.
代码:
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # @param head, a ListNode # @return a ListNode def deleteDuplicates(self, head): if head is None or head.next is None: return head dummyhead = ListNode(0) dummyhead.next = head p = dummyhead.next while p.next is not None: if p.val == p.next.val: p.next = p.next.next else: p = p.next return dummyhead.next
思路:
设立虚表头dummyhead
遇上val不同的,再执行 p=p.next;否则指针p不动,只是p.next变化。
相关文章推荐
- leetcode 【 Remove Duplicates from Sorted List II 】 python 实现
- 83. Remove Duplicates from Sorted List Leetcode Python
- LeetCode 之 Remove Duplicates from Sorted List I II — C++ 实现
- 【LeetCode with Python】 Remove Duplicates from Sorted List II
- LeetCode 83 — Remove Duplicates from Sorted List(C++ Java Python)
- C实现 LeetCode->Remove Duplicates from Sorted List II(枚举法)(单链表)
- [LeetCode]Remove Duplicates from Sorted List II@python
- 链表-python-leetcode 83 Remove Duplicates from Sorted List
- Leetcode-Remove Duplicates from Sorted List-Python
- C实现 LeetCode->Remove Duplicates from Sorted List (枚举法)(单链表去重复节点)
- [leetcode]Remove Duplicates from Sorted List II @ Python
- [LeetCode]题解(python):083-Remove Duplicates from Sorted List
- 【LeetCode with Python】 Remove Duplicates from Sorted List
- [LeetCode]Remove Duplicates from Sorted List@python
- [LeetCode]题解(python):082-Remove Duplicates from Sorted List II
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
- 【LeetCode 83】Remove Duplicates from Sorted List (Python)
- Python学习——leetcode(Remove Duplicates from Sorted List)
- 【LeetCode-面试算法经典-Java实现】【083-Remove Duplicates from Sorted List(排序的单链表中删除重复的结点)】
- 【LEETCODE】82- Remove Duplicates from Sorted List II [Python]