删除给出链表中的重复元素(链表中元素从小到大有序),使链表中的所有元素都只出现一次
例如:
给出的链表为
,返回
.
给出的链表为
,返回
.
例如:
给出的链表为
给出的链表为
数据范围:链表长度满足
,链表中任意节点的值满足
进阶:空间复杂度
,时间复杂度 )
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
struct ListNode* deleteDuplicates(struct ListNode* head )
{
// write code here
if(!head||head->next==NULL)
return head;
struct ListNode* temp=head;
while(temp!=NULL&&temp->next!= NULL)
{
if(temp->val==temp->next->val)
temp->next=temp->next->next;
else
temp=temp->next;
}
return head;
} /**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
struct ListNode* create(int value)
{
struct ListNode* new = (struct ListNode*)malloc(sizeof(struct ListNode));
new->val = value;
new->next = NULL;
return new;
}
struct ListNode* deleteDuplicates(struct ListNode* head ) {
// write code here
struct ListNode* new = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* cur = head, *p, *q;
if(cur == NULL || cur->next == NULL)
{
return head;
}
new->val = cur->val;
new->next = NULL;
q = new;
while(cur != NULL && cur->next != NULL)
{
if(cur->val == cur->next->val)
{
p = cur->next;
cur->next = NULL;
cur = p;
}
else
{
q->next = create(cur->val);
q = q->next;
cur = cur->next;
}
}
q->next = cur;
return new->next;
}
将不重复的元素提出来,然后放到一个新的链表中
//1.找到重复的结点
//2.删除它
typedef struct ListNode ListNode;
//传一个前驱指针,删除其后的结点
void ListPopBack(ListNode** prev)
{
if(!prev || !(*prev)->next)
{
return;
}
ListNode* dele = (*prev)->next;
ListNode* next = (*prev)->next->next;
(*prev)->next = next;
free(dele);
dele = NULL;
}
//遍历链表,查找与data重复的结点
//参数为需要查找的data,以及一个链表的结点
//返回值为重复结点的前驱指针prev
ListNode* ListResearch(ListNode* head, int x)
{
if(!head)
{
return NULL;
}
ListNode* prev = NULL;
ListNode* pcur = head;
while(pcur->next != NULL)
{
prev = pcur;
pcur = pcur->next;
if(pcur->val == x)
{
return prev;
}
}
if(pcur->val == x)
{
return prev;
}
//走到这里,说明没有与data重复的结点
return NULL;
}
struct ListNode* deleteDuplicates(struct ListNode* head )
{
//判断是否为空链表
if(!head)
{
return NULL;
}
//遍历整个链表
ListNode* compare = head;
ListNode* delePrev = NULL;
while(compare != NULL)
{
delePrev = ListResearch(compare, compare->val);
if(delePrev != NULL)
{
ListPopBack(&delePrev);
}
else
{
//检查后续结点
compare = compare->next;
}
}
return head;
} struct ListNode* deleteDuplicates(struct ListNode* head ) {
struct ListNode *res, *p=head->next;
if(head==NULL || head->next==NULL)
return head;
res = deleteDuplicates(head->next);
while(p!=NULL) {
if(p->val == head->val)
return res;
p = p->next;
}
head->next = res;
return head;
} struct ListNode* deleteDuplicates(struct ListNode* head) {
if (head == NULL) {
return head;
}
if (head->next == NULL) {
return head;
}
struct ListNode* p = head;
struct ListNode* q = head;
while (p != NULL && p->next != NULL) {
if (p->val == p->next->val) {
q = p->next;
p->next = q->next;
free(q); // 释放被删除的重复节点的内存
} else {
p = p->next;
}
}
return head;
} /**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
struct ListNode* deleteDuplicates(struct ListNode* head ) {
// write code here
if (head == NULL)return NULL;
struct ListNode* pList = head;
struct ListNode* tmp = NULL;
while (pList) {
tmp = pList;
if (pList->next && pList->val == pList->next->val)
{
pList->next = pList->next->next;
// 回退一个
if (pList->next && tmp->val == pList->next->val)
{
pList = tmp;
continue;
}
}
pList = pList->next;//取下一个元素
}
return head;
} struct ListNode* deleteDuplicates(struct ListNode* head ) {
// write code here
int temp;
struct ListNode* p = head;//修改链表指向
struct ListNode* p0 = head;//遍历被对比的元素
while (p0!= NULL && p0->next != NULL)
{
temp = p0->val;//从头开始取元素进行比较
//直到遇见下一个不重复的元素,否则被比较的指针向后移
while(p0->next->val == temp && p0->next != NULL)
{
p0 = p0->next;
continue;
}
p->next = p0->next;//使用p修改链表的指向,指向下一个不重复的元素
p = p->next;//p指针向后移
p0 = p0->next;//p0也向后移,进入下一个不重复的元素的比较
}
return head;
} struct ListNode* deleteDuplicates(struct ListNode* head )
{
if(head==NULL)
return NULL;
struct ListNode *p = head->next;
struct ListNode *q = head;
struct ListNode *tmp;
while(p!=NULL)
{
if(q->val==p->val)
{
tmp = p;
p = p->next;
q->next = p;
free(tmp);
continue;
}
q = q->next;
p = p->next;
}
return head;
} struct ListNode* deleteDuplicates(struct ListNode* head ) {
// write code here
if(head==NULL){
return head;
}
if(head->next == NULL) {
return head;
}
struct ListNode* p = head;
struct ListNode* q = head;
while(p!=NULL && p->next!=NULL) {
if(p->val==p->next->val) {
q = p->next;
p->next = q->next;
}else{
p=p->next;
}
}
return head;
}
struct ListNode* deleteDuplicates(struct ListNode* head) {
// write code here
if(head == NULL || head->next == NULL)
return head;
struct ListNode *tempNode;
struct ListNode *tempNodeNext;
tempNode = head;
while(tempNode) {
tempNodeNext = tempNode->next;
if(!tempNodeNext)
break;
if(tempNodeNext->val == tempNode->val) {
tempNode->next = tempNodeNext->next;
}
else {
tempNode = tempNodeNext;
}
}
return head;
} struct ListNode* deleteDuplicates(struct ListNode* head )
{
// write code here
if(head == NULL)
{
return NULL;
}
struct ListNode*p = head;
while(p)
{
while(p->next != NULL && p->val == p->next->val)//p的下一个与p的值相等
{
p->next = p->next->next;//删除p的下一个结点
}
p = p->next;
}
return head;
}