Untitled
unknown
python
a year ago
1.3 kB
10
Indexable
Never
class Node: def __init__(self, data=None): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if self.head is None: self.head = new_node else: current = self.head while current.next: current = current.next current.next = new_node def remove_duplicates(self): if self.head is None: return current = self.head while current: next_node = current.next if next_node and next_node.data == current.data: current.next = next_node.next else: current = current.next def to_list(self): result = [] current = self.head while current: result.append(current.data) current = current.next return result # Ví dụ sử dụng L = LinkedList() L.append(1) L.append(1) L.append(2) L.append(2) L.append(3) L.append(4) L.append(4) L.append(4) L.append(5) L.append(5) print("Danh sách ban đầu:", L.to_list()) L.remove_duplicates() print("Danh sách sau khi loại bỏ phần tử trùng lặp:", L.to_list())