题解 | #从单向链表中删除指定值的节点#

从单向链表中删除指定值的节点

https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f

package main

import (
	"fmt"
)

func main() {
	var count int
	fmt.Scan(&count)
	var headValue int
	fmt.Scan(&headValue)
	root := &List{Val: headValue, Next: nil}

	for i := 0; i < count-1; i++ {
		var a,b int
        fmt.Scan(&a, &b)
        head := root
		for head != nil {
			if head.Val == b {
                node := &List{Val: a}
				next := head.Next
                node.Next = next
                head.Next = node
			}
            head = head.Next
		}
	}

	var search int
	fmt.Scan(&search)
	for root != nil {
		if root.Val != search {
			fmt.Print(root.Val)
            fmt.Print(" ")
		}
        root = root.Next
	}
}

type List struct {
	Val  int
	Next *List
}

全部评论

相关推荐

06-11 17:39
门头沟学院 Java
小呆呆的大鼻涕:卧槽,用户彻底怒了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务