码迷,mamicode.com
首页 > 其他好文 > 详细

golang构造单链表

时间:2018-11-10 19:13:43      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:amp   struct   next   UNC   .com   imp   nbsp   pac   lis   

原文地址:http://www.niu12.com/article/47
package main

import "fmt"

type ListNode struct {
Value int
Next *ListNode
}

func main() {
one := makeListNode([]int{1, 2, 3})
for one != nil {
fmt.Println(one.Value)
one = one.Next
}
}

func makeListNode(nums []int) *ListNode {

if len(nums) == 0{
return nil
}

res := &ListNode{
Value:nums[0],
}

temp := res

for i := 1; i < len(nums); i++ {
temp.Next = &ListNode{Value:nums[i],}
temp = temp.Next
}

return res
}

 

golang构造单链表

标签:amp   struct   next   UNC   .com   imp   nbsp   pac   lis   

原文地址:https://www.cnblogs.com/zhouqi666/p/9940026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!