• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LilyLiya
博客园    首页    新随笔    联系   管理    订阅  订阅
Remove Duplicates From Linked List
sorted array, linklist

refer to: https://www.algoexpert.io/questions/Remove%20Duplicates%20From%20Linked%20List

Problem Statement

 

 Analysis

 

 Code

# This is an input class. Do not edit.
class LinkedList:
    def __init__(self, value):
        self.value = value
        self.next = None


def removeDuplicatesFromLinkedList(linkedList):
    currentNode = linkedList
    while currentNode is not None:
        nextDistinctNode = currentNode.next
        while nextDistinctNode is not None and nextDistinctNode.value == currentNode.value:
            nextDistinctNode = nextDistinctNode.next
            
        currentNode.next = nextDistinctNode# the first unique value(exclude the head node) found. let the head point to it
        currentNode = nextDistinctNode#update the current node
        
    return linkedList
        
            

Time and Space complexity

 

 

 
posted on 2021-06-02 08:18  LilyLiya  阅读(49)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3