此题可以选择暴力解决,首先将链表中的元素放到数组中,然后将数组中的重复元素放到另一个数组中,最后再判断并将目标值放到第三个数组中排序再改链表,注意链表next=NULL的操作
struct ListNode* deleteDuplicates(struct ListNode* head) {
if(head==NULL||head->next==NULL)
{
return head;;
}
int arr[1000];
struct ListNode*point=head;
int i=0;
while(point)
{
arr[i]=point->val;
i++;
point=point->next;
}
int arr1[1000];
int j=0;
for(int x=0;x<i;x++)
{
for(int y=0;y<x;y++)
{
if(arr[x]==arr[y])
{
arr1[j]=arr[x];
j++;
}
}
}
int arr2[1000];
int k=0;
for(int x=0;x<i;x++)
{
int count=0;
for(int y=0;y<j;y++)
{
if(arr1[y]==arr[x])
{
count++;
}
}
if(count==0)
{
arr2[k]=arr[x];
k++;
}
}
if(k==0)
{
head=NULL;
return head;
}
for(int x=0;x<k;x++)
{
for(int y=0;y<x;y++)
{
if(arr2[x]<arr2[y])
{
int d=arr2[x];
arr2[x]=arr2[y];
arr2[y]=d;
}
}
}
int t=0;
struct ListNode*good=head;
struct ListNode*bad;
while(t<k)
{
good->val=arr2[t];
t++;
bad=good;
good=good->next;
}
bad->next=NULL;
return head;
}