本文整理汇总了Golang中ctci/datastructures.LinkedList.Len方法的典型用法代码示例。如果您正苦于以下问题:Golang LinkedList.Len方法的具体用法?Golang LinkedList.Len怎么用?Golang LinkedList.Len使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ctci/datastructures.LinkedList
的用法示例。
在下文中一共展示了LinkedList.Len方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: IsPalindrome
// checks wheter given list is palindrome
func IsPalindrome(l ds.LinkedList) bool {
head := l.Head()
// reverse given list
reversed_list := reverseList(l)
reversed_head := reversed_list.Head()
// checks items until half of the list
for i := 0; i < l.Len()/2; i++ {
if head.GetValue() != reversed_head.GetValue() {
return false
}
head = head.Next()
reversed_head = reversed_head.Next()
}
return true
}
示例2: TestRemoveDuplicates
func TestRemoveDuplicates(t *testing.T) {
l := datastructures.LinkedList{}
for i := 0; i <= 10; i++ {
n := &datastructures.Node{}
n.SetValue(i % 3)
l.Add(n)
}
RemoveDuplicates(&l)
if l.Len() != 3 {
t.Error("Wrong length, Len() must be", 3)
}
if l.Find(0) == nil || l.Find(1) == nil || l.Find(2) == nil {
t.Error("Wrong deletion for one of the elements.")
}
}
示例3: SumLinkedListForward
// Sum up two linked list in forward order
// Input:(6 -> 1 -> 7) + (2 -> 9 -> 5).Thatis,617 + 295.
// Output: 9 -> 1 -> 2.That is, 912.
// we assume that, result integer does not exceed integer size
func SumLinkedListForward(l1 ds.LinkedList, l2 ds.LinkedList) int {
head1 := l1.Head()
head2 := l2.Head()
num1 := 0
num2 := 0
multiplier1 := exp(10, l1.Len()-1)
multiplier2 := exp(10, l2.Len()-1)
// convert l1 list to int
for head1 != nil {
num1 += multiplier1 * head1.GetValue()
multiplier1 = multiplier1 / 10
head1 = head1.Next()
}
// convert l2 list to int
for head2 != nil {
num2 += multiplier2 * head2.GetValue()
multiplier2 = multiplier2 / 10
head2 = head2.Next()
}
return num1 + num2
}