有双向循环链表结点定义为:
struct node
{
int data;
struct node *front,*next;
};
struct node
{
int data;
struct node *front,*next;
};
有两个双向循环链表A,B,知道其头指针为:pHeadA、pHeadB,请写一函数将两链表中data值相同的结点删除。
思路1: 遍历A的每个结点x,针对x,遍历B的每个结点,发现值相同,则用set<int>记录相同值。
记录了所有相同值后依次遍历A,B,删除与set<int>相同的结点,时间复杂度为O(n+m),空间复杂度为O(k),k为相同结点值数。
思路2:遍历A的每个结点,将其值和结点记录到hash_map<int, node*>中,然后遍历B,将相同data的部分删除,时间复杂度O(n+m),空间复杂度为O(n)。