本文整理汇总了Golang中go/chapter02/list.List类的典型用法代码示例。如果您正苦于以下问题:Golang List类的具体用法?Golang List怎么用?Golang List使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了List类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: isPalindromeUsingStack
//APPROACH - II
func isPalindromeUsingStack(l *list.List) bool {
if l == nil {
return false
}
st := stack.New()
slow := l.Front()
fast := l.Front()
for fast != nil && fast.Next() != nil {
st.Push(slow.Value.(int))
slow = slow.Next()
fast = fast.Next().Next()
}
//To ignore the middle element in case of odd length list
if fast.Next() == nil {
slow = slow.Next()
}
for slow != nil {
m := st.Pop().(int)
n := slow.Value.(int)
if m != n {
return false
}
slow = slow.Next()
}
return true
}
开发者ID:CR-Gjx,项目名称:ctci,代码行数:31,代码来源:question2_7.go
示例2: reverseList
//Reverse list function
func reverseList(l *list.List) *list.List {
m := list.New()
for e := l.Front(); e != nil; e = e.Next() {
m.PushFront(e.Value.(int))
}
return m
}
开发者ID:CR-Gjx,项目名称:ctci,代码行数:9,代码来源:question2_7.go
示例3: isPalindrome
//Palinfrome function using reverse list approach - Iterative
func isPalindrome(l *list.List) bool {
rev := reverseList(l)
for e, f := l.Front(), rev.Front(); e != nil && f != nil; e, f = e.Next(), f.Next() {
if e.Value.(int) != f.Value.(int) {
return false
}
}
return true
}
开发者ID:CR-Gjx,项目名称:ctci,代码行数:11,代码来源:question2_7.go
示例4: removeDuplicate
func removeDuplicate(l *list.List) *list.List {
sMap = make(map[int]bool)
var next *list.Element
for e := l.Front(); e != nil; e = next {
next = e.Next()
m := e.Value.(int)
//To verify whether the node value present in the map
if sMap[m] == true {
l.Remove(e)
} else {
sMap[m] = true
}
}
return l
}
开发者ID:quoidautre,项目名称:ctci,代码行数:15,代码来源:question2_1.go
示例5: splitAroundX
//Function to split the list around the value x
func splitAroundX(l *list.List, x int) *list.List {
if l == nil {
return nil
}
//Two lists for less than x and greater than X
lThanX := list.New()
gThanX := list.New()
for e := l.Front(); e != nil; e = e.Next() {
m := e.Value.(int)
//Compare the value of x with the current node value and append to the respective list
if m < x {
lThanX.PushBack(m)
} else {
gThanX.PushBack(m)
}
}
lThanX.PushBackList(gThanX)
return lThanX
}
开发者ID:quoidautre,项目名称:ctci,代码行数:21,代码来源:question2_4.go
示例6: removeDuplicate
func removeDuplicate(l *list.List) *list.List {
var next *list.Element
var inNext *list.Element
for e := l.Front(); e != nil; e = next {
m := e.Value.(int)
next = e.Next()
//Inner for loop to iterate from the current node
for f := e; f != nil; f = inNext {
n := f.Value.(int)
inNext = f.Next()
if e != f {
//To check whether the values m and n are same. If so, remove the node from the list
if m == n {
l.Remove(f)
}
}
}
}
return l
}
开发者ID:quoidautre,项目名称:ctci,代码行数:21,代码来源:question2_1b.go
示例7: addLists
//Function to add the list
func addLists(l *list.List, m *list.List) *list.List {
if l == nil && m == nil {
return nil
}
lLength := l.Len()
mLength := m.Len()
carry := 0
value := 0
resList := list.New()
var e *list.Element
var f *list.Element
for e, f = l.Front(), m.Front(); e != nil && f != nil; e, f = e.Next(), f.Next() {
value = carry + e.Value.(int) + f.Value.(int)
//get the carry and value
carry = 0
carry = value / 10
value = value % 10
resList.PushFront(value)
}
//To identify the long list if the size is different
var p *list.Element
if lLength > mLength {
p = e
} else {
p = f
}
for ; p != nil; p = p.Next() {
value = carry + p.Value.(int)
carry = 0
carry = value / 10
value = value % 10
resList.PushFront(value)
}
if carry != 0 {
resList.PushFront(carry)
}
return resList
}
开发者ID:quoidautre,项目名称:ctci,代码行数:41,代码来源:question2_5.go
示例8: findKFromLast
//Iterative function to find the kth from last element
func findKFromLast(l *list.List, k int) *list.Element {
size := l.Len()
//Base condition. If the size of the list is less than k then kth element cannot be found
if size < k {
return nil
}
var elem *list.Element
elem = l.Front()
for i := 1; i < k; i++ {
elem = elem.Next()
}
var first *list.Element
for first = l.Front(); first != nil && elem != nil; elem, first = elem.Next(), first.Next() {
//return the current node when current+k position is nil
if elem.Next() == nil {
return first
}
}
return nil
}
开发者ID:quoidautre,项目名称:ctci,代码行数:21,代码来源:question2_2.go
示例9: findLoopsInList
func findLoopsInList(l *list.List) *list.Element {
if l == nil {
return nil
}
var head *list.Element
var slow *list.Element
var fast *list.Element
head = l.Front()
for slow, fast = l.Front(), l.Front(); slow != nil && fast != nil; slow, fast = slow.Next(), fast.Next().Next() {
if slow == fast {
break
}
}
if fast == nil || fast.Next() == nil {
return nil
}
slow = head
for slow != fast {
slow = slow.Next()
fast = fast.Next()
}
return fast
}
开发者ID:CR-Gjx,项目名称:ctci,代码行数:24,代码来源:question2_6.go
注:本文中的go/chapter02/list.List类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论