Samsung Semiconductor diagnostic pipelines may represent ordered event identifiers as a singly linked list. Implement an in-place algorithm that removes every duplicate node while preserving the first occurrence and the relative order of remaining nodes.
Use the following node model:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
Implement remove_duplicates(head). The function receives the head node and must return the head of the modified list. A linked list such as [3, 1, 3, 2, 1] is represented in examples and tests by its serialized value array, but the submitted function operates on ListNode objects.
Do not allocate replacement nodes or use a set, dictionary, list, or other value-based buffer. Rewire existing next pointers directly. Keep the first node containing each value.
def remove_duplicates(head):