list

package
v0.0.0-...-9d323d5 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2021 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRevKthFromLinkList(head *LinkListNode, k int) (int, error)

GetRevKthFromLinkList 获取链表倒数第k个数

func IsLinkListLoop

func IsLinkListLoop(head *LinkListNode) bool

IsLinkListLoop 是否存在闭环,两种实现 1 增加一个外部存储(hash map)标记已遍历的节点看是否有重复 2 使用两个指针,采用不同的步进长度,如果两者相遇则表示有环路

func ReverseRange

func ReverseRange(from *ListNode, to *ListNode)

ReverseRange 原地反转(from, to)之间的链表,不包含from/to

Types

type DualLinkList struct {
	// contains filtered or unexported fields
}
func CreateDualLinkList() *DualLinkList

func (*DualLinkList) Append

func (dll *DualLinkList) Append(value int)

Append 追加到尾部

func (*DualLinkList) DeleteByValue

func (dll *DualLinkList) DeleteByValue(value int) *DualLinkListNode

DeleteByValue 根据取值删除

func (*DualLinkList) Find

func (dll *DualLinkList) Find(value int) *DualLinkListNode

func (*DualLinkList) Insert

func (dll *DualLinkList) Insert(value int)

Insert 插入到头部

func (*DualLinkList) Pop

func (dll *DualLinkList) Pop() *DualLinkListNode

Pop 从头部删除

func (*DualLinkList) Shift

func (dll *DualLinkList) Shift() *DualLinkListNode

Shift 从尾部删除

func (*DualLinkList) Visit

func (dll *DualLinkList) Visit(fn func(node *DualLinkListNode))

Visit 遍历高阶函数

type DualLinkListNode

type DualLinkListNode struct {
	// contains filtered or unexported fields
}
type LinkList struct {
	// contains filtered or unexported fields
}
func CreateLinkList() *LinkList
func MergeSortedLinkList(l1 *LinkList, l2 *LinkList) *LinkList

MergeSortedLinkList 合并有序链表 1: 归并排序,开一个新的链表,每次将小的放进去 2: 原地归并,使用其中一条作为基线进行归并,找到两者的小值之后,再在基线上移动游标寻找插入位置

func (*LinkList) Delete

func (ll *LinkList) Delete() *LinkListNode

Delete 从头部删除

func (*LinkList) DeleteByValue

func (ll *LinkList) DeleteByValue(value int) *LinkListNode

根据取值删除

func (*LinkList) Find

func (ll *LinkList) Find(value int) *LinkListNode

func (*LinkList) Insert

func (ll *LinkList) Insert(value int) *LinkListNode

Insert 插入到头部

func (*LinkList) InsertAfter

func (ll *LinkList) InsertAfter(value int, pos *LinkListNode)

func (*LinkList) Visit

func (ll *LinkList) Visit(fn func(node *LinkListNode))

Visit 遍历高阶函数

type LinkListNode

type LinkListNode struct {
	// contains filtered or unexported fields
}
func ReverseLinkList(head *LinkListNode) *LinkListNode

ReverseLinkList 原地反转链表 新建一个返回链表,next指向原链表头 每次循环:原链头所指向的元素移动到返回链头,并指向上次循环的返回链头

func (*LinkListNode) IsEqual

func (node *LinkListNode) IsEqual(target *LinkListNode) bool

type List

type List struct {
	// contains filtered or unexported fields
}

List 链表结构,首尾相连

func New

func New() *List

New 新建双链表

func (*List) Head

func (l *List) Head() *ListElement

Head 链表头节点

func (*List) InsertAfter

func (l *List) InsertAfter(v interface{}, pos *ListElement) *ListElement

InsertAfter 插入到指定位置之前

func (*List) InsertBefore

func (l *List) InsertBefore(v interface{}, pos *ListElement) *ListElement

InsertBefore 插入到指定位置之前

func (*List) Len

func (l *List) Len() int

Len 长度

func (*List) MoveAfter

func (l *List) MoveAfter(el, pos *ListElement)

MoveAfter 移动节点到指定位置之后

func (*List) MoveBefore

func (l *List) MoveBefore(el, pos *ListElement)

MoveBefore 移动节点到指定位置之前

func (*List) Remove

func (l *List) Remove(el *ListElement) interface{}

Remove 删除节点

func (*List) Tail

func (l *List) Tail() *ListElement

Tail 链表尾节点

type ListElement

type ListElement struct {
	Value interface{}
	// contains filtered or unexported fields
}

ListElement 链表节点,list指向链表

type ListNode

type ListNode struct {
	Val  int
	Next *ListNode
}

ListNode 单链表

func MergeKLists

func MergeKLists(lists []*ListNode) *ListNode

MergeKLists 合并k个有序链表 1. 采用小根堆存储每个链表的队头,每次从小根堆头部取节点加入返回链表中,并将该节点的Next放回小根堆中 2. 也可以使用分治法,将链表按照两两划分合并,然后递归合并到只剩一个链表

func SortList

func SortList(head *ListNode) *ListNode

SortList 链表排序,采用非原地快排,注意点: 1.基准选择头部节点,且要单独出来 2.新分组要切换队尾与原链表的关联 3.合并的时候要记得切断基准节点与原链表的关联

Jump to

Keyboard shortcuts

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