如何用链表删除二维数组中的连续重复元素?代码问题排查
问题:链表删除连续重复元素失效,仅能删除每个值的第一个重复项
我有一个包含重复元素的二维数组,希望使用链表删除其中的连续重复元素。目前代码仅能删除每个值的第一个重复项,代码如下:
from numpy import dot from numpy.linalg import norm class Node: def __init__(self, data=None, next=None): self.data = data self.next = next class LinkedList: def __init__(self): self.head = None def print(self): if self.head is None: print("Linked list is empty") return itr = self.head llstr = '' while itr: llstr += str(itr.data)+' --> ' if itr.next else str(itr.data) itr = itr.next print(llstr) def insert_at_end(self, data): if self.head is None: self.head = Node(data, None) return itr = self.head while itr.next: itr = itr.next itr.next = Node(data, None) def get_cos_sim(self, a, b): cos_sim = dot(a, b)/(norm(a)*norm(b)) return cos_sim def remove_duplicates(self, head): temp = head while temp.next != None: get_first_node = temp.data second_node = temp.next get_second_node = second_node.data cos_sim_value = self.get_cos_sim(get_first_node, get_second_node) if cos_sim_value >= 0.99: temp.next = temp.next.next temp = temp.next else: temp = temp.next return temp def insert_values(self, data_list): self.head = None for data in data_list: self.insert_at_end(data) test_arr = [ [0.11, 2.6875], [0.11, 2.6875], [100, 200], [100, 200], [100, 200], [0.11, 2.6875], [300, 400], [300, 400], [300, 400] ] ll = LinkedList() ll.insert_values(test_arr) ll.remove_duplicates(ll.head) ll.print()
当前输出:
[0.11, 2.6875] --> [100, 200] --> [100, 200] --> [0.11, 2.6875] --> [300, 400] --> [300, 400]
期望输出:
[0.11, 2.6875] --> [100, 200] --> [0.11, 2.6875] --> [300, 400]
错误分析与修正
核心错误点
- 删除重复后错误移动指针:当删除
temp.next后,立即执行temp = temp.next,导致跳过了对新的temp.next的检查。比如连续三个相同元素时,删完第二个后,temp移到第三个,直接跳过了第三个与第一个的重复检查,残留重复项。 - 语法缩进错误:
else块的缩进与while对齐,导致逻辑错误,应与if对齐。 - 返回值无意义:
remove_duplicates返回temp,但实际需要保留链表头节点,返回head才合理。
修正后的代码
from numpy import dot from numpy.linalg import norm class Node: def __init__(self, data=None, next=None): self.data = data self.next = next class LinkedList: def __init__(self): self.head = None def print(self): if self.head is None: print("Linked list is empty") return itr = self.head llstr = '' while itr: llstr += str(itr.data)+' --> ' if itr.next else str(itr.data) itr = itr.next print(llstr) def insert_at_end(self, data): if self.head is None: self.head = Node(data, None) return itr = self.head while itr.next: itr = itr.next itr.next = Node(data, None) def get_cos_sim(self, a, b): cos_sim = dot(a, b)/(norm(a)*norm(b)) return cos_sim def remove_duplicates(self, head): temp = head while temp and temp.next: # 同时判断temp和temp.next不为空,避免空指针异常 get_first_node = temp.data second_node = temp.next get_second_node = second_node.data cos_sim_value = self.get_cos_sim(get_first_node, get_second_node) if cos_sim_value >= 0.99: # 删除重复节点后,temp不移动,继续检查新的next temp.next = temp.next.next else: # 无重复时才移动temp指针 temp = temp.next return head # 返回头节点,保持链表入口 def insert_values(self, data_list): self.head = None for data in data_list: self.insert_at_end(data) test_arr = [ [0.11, 2.6875], [0.11, 2.6875], [100, 200], [100, 200], [100, 200], [0.11, 2.6875], [300, 400], [300, 400], [300, 400] ] ll = LinkedList() ll.insert_values(test_arr) ll.remove_duplicates(ll.head) ll.print()
修正说明
- 删除重复节点后,
temp不移动,继续检查当前temp与新的temp.next是否重复,确保所有连续重复项都被删除。 - 修正
else块缩进,确保逻辑正确:只有当前节点与下一个节点不重复时,才移动temp指针。 - 循环条件改为
while temp and temp.next,避免空指针访问异常。
运行修正后的代码,输出将符合预期:
[0.11, 2.6875] --> [100, 200] --> [0.11, 2.6875] --> [300, 400]
内容的提问来源于stack exchange,提问作者merkle
相关产品推荐
相关产品推荐

