题解 | #删除有序链表中重复的元素-II#

删除有序链表中重复的元素-II

http://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024


package main
import . "nc_tools"
/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */

/**
  * 
  * @param head ListNode类 
  * @return ListNode类
*/
func deleteDuplicates( head *ListNode ) *ListNode {
    // write code here
    
    if head == nil || head.Next == nil  {
        return head 
    }
    
    ht1 := make(map[int]bool)
    ht2 := make(map[int]bool)
    
    tmp := head 
    for tmp != nil {
        if ht1[tmp.Val] {
            ht2[tmp.Val] = true 
        }
        ht1[tmp.Val] = true  
        tmp = tmp.Next 
    }
    
    tmp = head 
    for tmp != nil && ht2[tmp.Val] {
        tmp = tmp.Next 
    }
    
    head = tmp 
    tmp = head 
    
    for tmp != nil {
        for tmp.Next != nil && ht2[tmp.Next.Val] {
            tmp.Next = tmp.Next.Next
        }
        tmp = tmp.Next 
    }
    
    return head 
}
全部评论

相关推荐

点赞 评论 收藏
分享
06-26 15:33
青岛工学院 Java
积极的秋田犬要冲国企:他现在邀请我明天面试
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 11:35
程序员小白条:话太多,没实力和学历,差不多回答回答就行了,身份地位不一样
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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