删除排序链表中的重复元素
- https://leetcode.cn/problems/remove-duplicates-from-sorted-list/
描述
- 给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表
示例 1
输入:head = [1,1,2]
输出:[1,2]
示例 2
输入:head = [1,1,2,3,3]
输出:[1,2,3]
提示
- 链表中节点数目在范围 [0, 300] 内
- -100 <= Node.val <= 100
- 题目数据保证链表已经按升序排列
Typescript 版算法实现
1 )方案 1
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function deleteDuplicates(head: ListNode | null): ListNode | null {
let p = head;
while(p?.next) {
// 检测相邻两位是否相等
const isEqual = p.val === p.next.val
// 相等则移动两位,否则正常移动一位
isEqual ? (p.next = p.next.next) : (p = p.next)
}
// 原路返回head
return head;
};
-
解题思路
- 链表是有序的,重复链表一定相邻
- 遍历链表,如果发现当前元素和下个元素值相同,就删除下个元素值
- 删除的方式就是当前元素直接链接下下个元素
-
解题步骤
- 遍历链表,如果发现当前元素和下个元素值相同就删除下个元素值
- 遍历结束后,返回原链表头即可
2 )方案 2
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function deleteDuplicates(head: ListNode | null): ListNode | null {
let cur = head
let next = cur
while(cur) {
do {
next = next.next
} while(next && cur.val === next.val)
cur.next = next
cur = cur.next
}
return head
};
- 上述是官方示例程序
- 这里while里面嵌套do while,这种写法让人眼前一亮
Python3 版算法实现
1 )方案1
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
# 如果链表为空或者只有一个节点,则直接返回
if not head or not head.next:
return head
p = head
while p.next:
# 检测相邻两位是否相等
if p.val == p.next.val:
# 相等则删除下一个节点
p.next = p.next.next
else:
# 不相等则正常移动一位
p = p.next
# 返回头节点
return head
2 )方案2
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
cur = head
while cur:
next_node = cur.next
while next_node and cur.val == next_node.val:
next_node = next_node.next
cur.next = next_node
cur = cur.next
return head
Golang 版算法实现
1 ) 方案1
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func deleteDuplicates(head *ListNode) *ListNode {
p := head
for p != nil && p.Next != nil {
// 检测相邻两位是否相等
isEqual := p.Val == p.Next.Val
// 相等则移动两位,否则正常移动一位
if isEqual {
p.Next = p.Next.Next
} else {
p = p.Next
}
}
// 原路返回head
return head
}
2 )方案2
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func deleteDuplicates(head *ListNode) *ListNode {
cur := head
for cur != nil {
next := cur.Next
for next != nil && cur.Val == next.Val {
next = next.Next
}
cur.Next = next
cur = cur.Next
}
return head
}
1万+



被折叠的 条评论
为什么被折叠?



