文章目录
- AcWing 3756. 筛选链表
- AC代码
AcWing 3756. 筛选链表
本题链接:AcWing 3756. 筛选链表
注:链接题目仅代表和本题大体相似
因为是考研笔试,本题代码以C语言去写
AC代码
代码解释:遍历链表,如果该节点的值出现过,那么就删除掉该节点,否则把该节点对应的值存入到判重数组st
中
代码:
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; * * Note: The returned array must be malloced, assume caller calls free(). */ int abs(int a){ return a > 0 ? a : -a; } struct ListNode* filterList(struct ListNode* head) { struct ListNode *p, *q; int st[10010] = {0}; st[abs(head -> val)] = 1; for (p = head; p -> next;){ int x = abs(p -> next -> val); if (st[x]){ q = p -> next; p -> next = q -> next; free(q); } else{ p = p -> next; st[x] = 1; } } return head; }