题解 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
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 }