题解 | #牛牛的递增之旅#

牛牛的递增之旅

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

简化版的删除重复节点,无需prev指针。

package main
import "fmt"
import . "nc_tools"
/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */
func removeDuplicates( head *ListNode ) *ListNode {
    if head == nil{
        fmt.Println()
        return head
    }
    curr, next := head, head.Next
    for next != nil{
        if curr.Val == next.Val{
            curr.Next = next.Next
            next.Next = nil
            next = curr.Next
        }else{
            curr, next = next, next.Next
        }
    }
    return head
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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