本文整理汇总了Golang中ctci/datastructures.LinkedList.SetHead方法的典型用法代码示例。如果您正苦于以下问题:Golang LinkedList.SetHead方法的具体用法?Golang LinkedList.SetHead怎么用?Golang LinkedList.SetHead使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ctci/datastructures.LinkedList
的用法示例。
在下文中一共展示了LinkedList.SetHead方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Slice
// Gives sliced linked list from kth element to last element.
// Also it assumes that, we don't know size&last element of the linkedlist.
func Slice(l datastructures.LinkedList, kth int) *datastructures.LinkedList {
if kth <= 0 {
return nil
}
current := l.Head()
runner := current
sliced_list := datastructures.LinkedList{}
count := 0
for current != nil {
runner = current
if count == kth {
sliced_list.SetHead(current)
for runner != nil {
runner = runner.Next()
}
break
sliced_list.SetTail(current)
}
count++
current = current.Next()
}
if sliced_list.Head() != nil {
return &sliced_list
} else {
return nil
}
}
示例2: reverseList
// reverse given list
func reverseList(l ds.LinkedList) ds.LinkedList {
temp := l.Copy()
reversed_list := ds.LinkedList{}
head := temp.Head()
current_node := head
var prev_node *ds.Node
for {
if current_node != nil {
next_node := current_node.Next()
current_node.SetNext(prev_node)
prev_node = current_node
current_node = next_node
} else {
reversed_list.SetHead(prev_node)
break
}
}
return reversed_list
}