• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang list.List类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang constant.BinaryOp函数代码示例发布时间:2022-05-28
下一篇:
Golang build.Package类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap