https://leetcode.com/problems/populating-next-right-pointers-in-each-node


Runtime: 4 ms, faster than 94.83% of Go online submissions for Populating Next Right Pointers in Each Node.
Memory Usage: 6.6 MB, less than 40.95% of Go online submissions for Populating Next Right Pointers in Each Node.


```go

/**
 * Definition for a Node.
 * type Node struct {
 *     Val int
 *     Left *Node
 *     Right *Node
 *     Next *Node
 * }
 */

func connect(root *Node) *Node {
	//5:31
	if root == nil {
		return nil
	}

	level_list := make([][]*Node, 0)
    level_list = append(level_list, []*Node{root})

	for true {
		current_level := level_list[len(level_list)-1]
		level_list = append(level_list, make([]*Node, 0))

		for _, each := range current_level {
			if each.Left != nil {
				level_list[len(level_list)-1] = append(level_list[len(level_list)-1], each.Left)
			}
			if each.Right != nil {
				level_list[len(level_list)-1] = append(level_list[len(level_list)-1], each.Right)
			}
		}

		if len(level_list[len(level_list)-1]) == 0 {
			level_list = level_list[0 : len(level_list)-1]
			break
		}

	}
	for _, each_level := range level_list {
		for index, node := range each_level {
			if index == len(each_level)-1 {
				node.Next = nil
			} else {
				node.Next = each_level[index+1]
			}
		}
	}

	return level_list[0][0]
	//5:37
}
```