lc083

package
v0.0.0-...-b071cee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2023 License: GPL-3.0 Imports: 2 Imported by: 0

README

83.删除排序链表中的重复元素

1. 题目描述

给定一个已排序的链表的头 head删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表

示例 1:


输入:head = [1,1,2]
输出:[1,2]

示例 2:


输入:head = [1,1,2,3,3]
输出:[1,2,3]

提示:

  • 链表中节点数目在范围 [0, 300]
  • -100 <= Node.val <= 100
  • 题目数据保证链表已经按升序 排列

标签 链表

2. 解题

由于给定的链表是排好序的,我们只需要对链表进行一次遍历,就可以删除重复的元素。设哑结点dummy指向链表的头节点,设p指针指向head,随后开始对链表进行遍历。
若p.Val与 p.next.Val对应的元素相同,那么我们就将p.next节点从链表中移除;否则说明链表中已经不存在其它与p指针对应的元素相同的节点。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ListNode

type ListNode struct {
	Val  int
	Next *ListNode
}

func BuildCircleListWithNoHead

func BuildCircleListWithNoHead(list []int, cursor int) *ListNode

BuildCircleListWithNoHead build a circle list, which tail point to list[cursor]

func BuildListWithHead

func BuildListWithHead(data []int) *ListNode

func BuildListWithNoHead

func BuildListWithNoHead(nums []int) *ListNode

func (*ListNode) HasCycle

func (l *ListNode) HasCycle() (bool, *ListNode)

HasCycle check if linklist has cycle structure, if not, return false, nil, if yes, return true and the cycle start node

func (*ListNode) Index

func (l *ListNode) Index(target int) *ListNode

Index return the node which value equals target

func (*ListNode) Len

func (l *ListNode) Len() int

Len return list length, when list has cycle structure, return -1

func (*ListNode) Slice

func (l *ListNode) Slice() []int

Slice return slice which contains all node's values in order.

func (*ListNode) String

func (l *ListNode) String() string

String implements Stringer interface.

func (*ListNode) Tail

func (l *ListNode) Tail() *ListNode

Tail return last list node

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL