[LeetCode]题解(python):083 - Remove Duplicates from Sorted List

题目来源



https://leetcode.com/problems/remove-duplicates-from-sorted-list/

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.



题意分析



Input:

:type head: ListNode

Output:

:rtype: ListNode

Conditions:一个有序list,删除掉重复的元素



题目思路



每次判断增加一个节点时,看是否与当前结点重复,重复则跳过



AC代码(Python)

 1 # Definition for singly-linked list.
 2 # class ListNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6
 7 class Solution(object):
 8     def deleteDuplicates(self, head):
 9         """
10         :type head: ListNode
11         :rtype: ListNode
12         """
13         if head == None or head.next == None:
14             return head
15         p = head
16         while p.next != None:
17             if p.val == p.next.val:
18                 p.next = p.next.next
19             else:
20                 p = p.next
21         return head
22             
时间: 2024-10-11 15:53:25

[LeetCode]题解(python):083 - Remove Duplicates from Sorted List的相关文章

LeetCode记录之26——Remove Duplicates from Sorted Array

国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等.拿起键盘干就行了.然后返回有序项的下标就可以. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not

[Leetcode][Python]26: Remove Duplicates from Sorted Array

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 26: Remove Duplicates from Sorted Arrayhttps://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the duplicates in place such that each element appear

<LeetCode OJ> 83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: Easy 题目意思:如今有一个已经排好顺序的链表,删除全部反复的节点.使每一个节点都仅仅出现一次! Given a sorted linked list, delete all duplicates such that each element appear only once. For exampl

LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II

1. Remove Duplicates from Sorted List 题目链接 题目要求: 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. 这道题不难.具体程序

LeetCode(2) Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array 题目 考察数组 描述 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with const

083. Remove Duplicates from Sorted List

题目链接:https://leetcode.com/problems/rotate-list/description/ Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2 Example 2: Input: 1->1->2->3->3 Output: 1-&g

LeetCode(26) Remove Duplicates from Sorted Array

题目 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array n

leetcode第26题--Remove Duplicates from Sorted Array

problem: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input ar

LeetCode【83】Remove Duplicates from Sorted List

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. 简单的题目,直接上AC代码: ListNode* deleteDuplicates(ListNode* head) { i