题解 | #删除有序链表中重复的元素-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 
}
全部评论

相关推荐

书海为家:实习是成为大厂正式员工很好的敲门砖,看您的简历中有一段实习经历,挺好的。我来给一点点小建议,因为毕竟还在学校不像工作几年的老鸟有丰富的项目经验,面试官在面试在校生的时候更关注咱们同学的做事逻辑和思路,所以最好在简历中描述下自己实习时做过项目的完整过程,比如需求怎么来的,你对需求的解读,你想到的解决办法,遇到困难如何找人求助,最终项目做成了什么程度,你从中收获了哪些技能,你有什么感悟。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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