/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/*
函数主要是两层循环,外层循环实现遍历整个链表,内层循环负责遍历比较当前节点值与其后所有节点值,若相等则跳过,
*/
struct ListNode* removeDuplicateNodes(struct ListNode* head) {
// Return NULL if the list is empty
if (head == NULL) return NULL;
struct ListNode *current = head;
// Iterate through each node in the list
while (current) {
struct ListNode* p = current;
// Check subsequent nodes for duplicates
while (p->next) {
// If a duplicate is found, remove it
if (p->next->val == current->val) {
p->next = p->next->next;
} else {
// Otherwise, move to the next node
p = p->next;
}
}
// Move to the next node in the list
current = current->next;
}
// Return the modified list
return head;
}