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

go中单链表

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

import (
	"fmt"
)

type ListNode struct {
	Val  int
	Next *ListNode
}
type List struct {
	headNode *ListNode //头节点
}

func main() {

	//res := letcode.Divide(434, 423)
	//fmt.Println(res)

	headNode := &ListNode{}

	listData := headNode
	Insert(1, listData, headNode)
	Insert(2, listData, headNode)
	Insert(4, listData, headNode)

	res := Find(2, listData)
	fmt.Println(res)

	//listData2 := headNode
	//
	//Insert(4, listData2, headNode)
	//Insert(3, listData2, headNode)
	//Insert(2, listData2, headNode)

	//PrintList(listData)

	//res := MergeTwoLists(listData, listData2)
	//fmt.Println(res)
}

//删除节点
func Delete(value int, list *ListNode) {
	pPrev := FindPrevious(value, list)
	_ = pPrev
	p := Find(value, list)
	pPrev = p.Next
	p = nil
}

// FindPrevious ...
func FindPrevious(value int, list *ListNode) *ListNode {
	p := list
	for p.Next != nil && p.Next.Val != value {
		p = p.Next
	}
	return p
}

// 找出链表里面的(不安全)
func Find(value int, list *ListNode) *ListNode {

	p := list
	for p.Val != value {
		p = p.Next
	}
	return p

}

// 测试是否为最后节点 ...
func IsLast(list *ListNode) bool {
	return list.Next == nil
}

// 测试链表是否为空 ...
func isEmpty(list *ListNode) bool {
	return list == nil
}

// 打印链表 ...
func PrintList(list *ListNode) {

	if list.Next != nil {
		fmt.Println(list.Val)
		PrintList(list.Next)
	} else {
		fmt.Println(list.Val)
	}
}

// 合并两个有序的单链表 ...
func MergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
	if l1 == nil {
		return l2
	}
	if l2 == nil {
		return l1
	}
	var res *ListNode
	if l1.Val >= l2.Val {
		res = l2
		res.Next = MergeTwoLists(l1, l2.Next)
	} else {
		res = l1
		res.Next = MergeTwoLists(l1.Next, l2)
	}
	return res
}

// 插入节点 头插法
func Insert(value int, list *ListNode, position *ListNode) {
	tempCell := new(ListNode)
	//fmt.Println("++++", tempCell)
	if tempCell == nil {
		fmt.Println("out of space!")
	}
	tempCell.Val = value
	tempCell.Next = position.Next
	position.Next = tempCell
}

  


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
惊人go语言(image网站开发)发布时间:2022-07-10
下一篇:
Go语言之进阶篇爬百度贴吧并发版发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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