package singlylinkedlist
import("fmt")type Node struct{
value int
next *Node
}funcNewNode(value int, next *Node)*Node {return&Node{value, next}}type singlylinkedlist struct{
head *Node
size int}//// NewList// @Description: 初始化单链表,指定头结点和大小// @param head// @param size// @return *singlylinkedlist//funcNewList(head *Node, size int)*singlylinkedlist {return&singlylinkedlist{head, size}}//// display// @Description: 打印出单链表中所有结点的值// @receiver l//func(l *singlylinkedlist)display(){if l.isEmpty(){
fmt.Println("The list to display is empty")}
cur := l.head
for cur !=nil{if cur.next ==nil{
fmt.Println(cur.value)break}
fmt.Print(cur.value,"->")
cur = cur.next
}}//// detectLoop// @Description: 判断单链表中是否存在环(使用快慢指针的方式)// @return bool//func(l *singlylinkedlist)detectLoop()bool{
slow := l.head
fast := l.head
for(fast !=nil)&&(fast.next !=nil){
slow = slow.next
fast = fast.next.next
if fast == slow {returntrue}}returnfalse}//// reverseList// @Description: 就地翻转单链表// @receiver l//func(l *singlylinkedlist)reverseList(){var pre *Node
cur := l.head
nxt := l.head
for cur !=nil{
nxt = cur.next
cur.next = pre
pre = cur
cur = nxt
}
l.head = pre
}//// clear// @Description: 清除单链表中所有结点// @receiver l// @return res//func(l *singlylinkedlist)clear()(res []int){
cur := l.head
for cur !=nil{
prev := cur
res =append(res, prev.value)
cur = cur.next
prev =nil}
l.head =nil
l.size =0return res
}//// isEmpty// @Description: 判断单链表是否为空// @receiver l// @return bool//func(l *singlylinkedlist)isEmpty()bool{return l.size ==0}//// count// @Description: 返回单链表的长度大小// @receiver l// @return int the count of the list//func(l *singlylinkedlist)length()int{return l.size
}//// getHead// @Description: 返回单链表的首结点// @receiver l// @return *Node the head of the list//func(l *singlylinkedlist)getHead()*Node {return l.head
}//// setHead// @Description: 设置单链表的首结点// @receiver l// @param node//func(l *singlylinkedlist)setHead(node *Node){
l.head = node
}//// count// @Description: 手动计算单链表的长度大小// @receiver l// @return int the count of the list//func(l *singlylinkedlist)count()int{
count :=0
cur := l.head
for cur !=nil{
count++
cur = cur.next
}return count
}//// search// @Description: 判断所给的key值是否在单链表中// @receiver l// @param key the value to be searched// @return bool {@code true} if key is present in the list, otherwise {@code false}//func(l *singlylinkedlist)search(key int)bool{
cur := l.head
for cur !=nil{if cur.value == key {returntrue}
cur = cur.next
}returnfalse}//// getNth// @Description: 根据所给的index返回单链表中相应的元素值// @receiver l// @param index// @return int//func(l *singlylinkedlist)getNth(index int)int{
l.checkBounds(index,0, l.size -1)
cur := l.head
for i :=0; i < index; i++{
cur = cur.next
}return cur.value
}//// insertHead// @Description: 在头结点前插入元素// @receiver l// @param x//func(l *singlylinkedlist)insertHead(x int){
l.insertNth(x,0)}//// insertTail// @Description: 在尾结点之后插入元素// @receiver l// @param data//func(l *singlylinkedlist)insertTail(data int){
l.insertNth(data, l.size)}//// insertNth// @Description: 在指定的位置上插入元素// @receiver l// @param data// @param position//func(l *singlylinkedlist)insertNth(data int, position int){
l.checkBounds(position,0, l.size)
newNode :=NewNode(data,nil)if l.head ==nil{
l.head = newNode
l.size++return}if position ==0{
newNode.next = l.head
l.head = newNode
l.size++return}
cur := l.head
for i :=0; i < position -1; i++{
cur = cur.next
}
newNode.next = cur.next
cur.next = newNode
l.size++}//// deleteHead// @Description: 删除头结点// @receiver l// @return int//func(l *singlylinkedlist)deleteHead()int{return l.deleteNth(0)}//// deleteTail// @Description: 删除尾结点// @receiver l// @return int//func(l *singlylinkedlist)deleteTail()int{return l.deleteNth(l.size -1)}//// deleteNth// @Description: 删除指定位置的元素并返回其响应的元素值// @receiver l// @param position// @return int//func(l *singlylinkedlist)deleteNth(position int)int{
l.checkBounds(position,0, l.size -1)if position ==0{
destroy := l.head
deleteValue := destroy.value
l.head = l.head.next
destroy =nil
l.size--return deleteValue
}
cur := l.head
// 找到需要删除的元素的前一个位置for i :=0; i < position -1; i++{
cur = cur.next
}
destroy := cur.next
deleteValue := destroy.value
cur.next = cur.next.next
destroy =nil
l.size--return deleteValue
}//// checkBounds// @Description: 判断所给的position是否超过了单链表的范围// @receiver l// @param position// @param low// @param high//func(l *singlylinkedlist)checkBounds(position int, low int, high int){if position > high || position < low {panic("IndexOutOfBound")}}
package singlylinkedlist
import("fmt")type Node struct{
value int
next *Node
}funcNewNode(value int, next *Node)*Node {return&Node{value, next}}type singlylinkedlist struct{
head *Node
size int}//// NewList// @Description: Init singly lined list with specified head and size// @param head// @param size// @return *singlylinkedlist//funcNewList(head *Node, size int)*singlylinkedlist {return&singlylinkedlist{head, size}}//// display// @Description: Display the value of each node in singly linked list// @receiver l//func(l *singlylinkedlist)display(){if l.isEmpty(){
fmt.Println("The list to display is empty")}
cur := l.head
for cur !=nil{if cur.next ==nil{
fmt.Println(cur.value)break}
fmt.Print(cur.value,"->")
cur = cur.next
}}//// detectLoop// @Description: Detect there is a loop in singly linked list (using slow&fast pointer)// @return bool//func(l *singlylinkedlist)detectLoop()bool{
slow := l.head
fast := l.head
for(fast !=nil)&&(fast.next !=nil){
slow = slow.next
fast = fast.next.next
if fast == slow {returntrue}}returnfalse}//// reverseList// @Description: Reverse a singly linked list// @receiver l//func(l *singlylinkedlist)reverseList(){var pre *Node
cur := l.head
nxt := l.head
for cur !=nil{
nxt = cur.next
cur.next = pre
pre = cur
cur = nxt
}
l.head = pre
}//// clear// @Description: Clear all nodes in singly linked list// @receiver l// @return res//func(l *singlylinkedlist)clear()(res []int){
cur := l.head
for cur !=nil{
prev := cur
res =append(res, prev.value)
cur = cur.next
prev =nil}
l.head =nil
l.size =0return res
}//// isEmpty// @Description: Check if the list is empty// @receiver l// @return bool//func(l *singlylinkedlist)isEmpty()bool{return l.size ==0}//// count// @Description: Return the size of list// @receiver l// @return int the count of the list//func(l *singlylinkedlist)length()int{return l.size
}//// getHead// @Description: Return head of the list// @receiver l// @return *Node the head of the list//func(l *singlylinkedlist)getHead()*Node {return l.head
}//// setHead// @Description: Set head of the list// @receiver l// @param node//func(l *singlylinkedlist)setHead(node *Node){
l.head = node
}//// count// @Description: Calculate count of the list manually// @receiver l// @return int the count of the list//func(l *singlylinkedlist)count()int{
count :=0
cur := l.head
for cur !=nil{
count++
cur = cur.next
}return count
}//// search// @Description: Test if the value key is present in the list// @receiver l// @param key the value to be searched// @return bool {@code true} if key is present in the list, otherwise {@code false}//func(l *singlylinkedlist)search(key int)bool{
cur := l.head
for cur !=nil{if cur.value == key {returntrue}
cur = cur.next
}returnfalse}//// getNth// @Description: Return element at special index// @receiver l// @param index// @return int//func(l *singlylinkedlist)getNth(index int)int{
l.checkBounds(index,0, l.size -1)
cur := l.head
for i :=0; i < index; i++{
cur = cur.next
}return cur.value
}//// insertHead// @Description: Insert an element at the head of the list// @receiver l// @param x//func(l *singlylinkedlist)insertHead(x int){
l.insertNth(x,0)}//// insertTail// @Description: Insert an element at the tail of the list// @receiver l// @param data//func(l *singlylinkedlist)insertTail(data int){
l.insertNth(data, l.size)}//// insertNth// @Description: Insert a new node at a specified position of the list// @receiver l// @param data// @param position//func(l *singlylinkedlist)insertNth(data int, position int){
l.checkBounds(position,0, l.size)
newNode :=NewNode(data,nil)if l.head ==nil{
l.head = newNode
l.size++return}if position ==0{
newNode.next = l.head
l.head = newNode
l.size++return}
cur := l.head
for i :=0; i < position -1; i++{
cur = cur.next
}
newNode.next = cur.next
cur.next = newNode
l.size++}//// deleteHead// @Description: Delete a node at the head// @receiver l// @return int//func(l *singlylinkedlist)deleteHead()int{return l.deleteNth(0)}//// deleteTail// @Description: Delete an element at the tail// @receiver l// @return int//func(l *singlylinkedlist)deleteTail()int{return l.deleteNth(l.size -1)}//// deleteNth// @Description: Delete an element at Nth position and return the deleted value// @receiver l// @param position// @return int//func(l *singlylinkedlist)deleteNth(position int)int{
l.checkBounds(position,0, l.size -1)if position ==0{
destroy := l.head
deleteValue := destroy.value
l.head = l.head.next
destroy =nil
l.size--return deleteValue
}
cur := l.head
// To find the node before the specified positionfor i :=0; i < position -1; i++{
cur = cur.next
}
destroy := cur.next
deleteValue := destroy.value
cur.next = cur.next.next
destroy =nil
l.size--return deleteValue
}//// checkBounds// @Description: Check if the position in the range// @receiver l// @param position// @param low// @param high//func(l *singlylinkedlist)checkBounds(position int, low int, high int){if position > high || position < low {panic("IndexOutOfBound")}}