网站卡密代理怎么做,sem搜索引擎,湖州做网站公司哪家好,柯桥做网站的公司2.1 编写代码#xff0c;移除未排序链表中的重复节点。 不使用临时缓存#xff1a; 如果不允许使用临时的缓存(即不能使用额外的存储空间)#xff0c;那需要两个指针#xff0c; 当第一个指针指向某个元素时#xff0c;第二个指针把该元素后面与它相同的元素删除#xff…2.1 编写代码移除未排序链表中的重复节点。 不使用临时缓存 如果不允许使用临时的缓存(即不能使用额外的存储空间)那需要两个指针 当第一个指针指向某个元素时第二个指针把该元素后面与它相同的元素删除 时间复杂度O(n2 )。 C实现代码 #includeiostream
#includenew
using namespace std;struct ListNode
{int val;ListNode *next;ListNode(int x):val(x),next(NULL) {}
};void createList(ListNode *L)
{int arr[10] {1,2,3,2,5,6,7,3,9,1};int i;ListNode *pNULL;for(i0; i10; i){ListNode *tmpnew ListNode(arr[i]);if(LNULL){Ltmp;ptmp;}else{p-nexttmp;ptmp;}}
}void deleteDup(ListNode *L)
{if(LNULL)return;ListNode *pL;ListNode *qNULL;while(p){qp;while(q-next){if(q-next-valp-val)q-nextq-next-next;elseqq-next;}pp-next;}
}int main()
{ListNode *headNULL;createList(head);ListNode *phead;while(p){coutp-val ;pp-next;}coutendl;deleteDup(head);phead;while(p){coutp-val ;pp-next;}coutendl;
} 运行结果