单链表——移除单链表中的元素

给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。

思路1:添加哨兵位头节点,双指针遍历

思路2:把不是val的尾插到新链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* removeElements(struct ListNode* head, int val)
{
    struct ListNode* newnode = (struct ListNode*)malloc(sizeof(struct ListNode));
    newnode->next = head;
    struct ListNode* cur = head;
    struct ListNode* prev = newnode;
    while(cur)
    {
        if(cur->val == val)
        {
            prev->next = cur->next;
        } 
        else 
        {
            prev = cur;
        }
        cur = cur->next;
    }
    newhead = newnode->next;
    free(newnode);
    return newhead;
}

//或者
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* removeElements(struct ListNode* head, int val)
{
    if(head == NULL)
        return NULL;
    struct ListNode* cur = head;
    struct ListNode* prev = NULL;
    while(cur)
    {
        if(cur->val == val)
        {
            struct ListNode* next = cur->next;
            if(prev == NULL)
            {
                head = cur->next;
            }
            else
            {
                prev->next = cur->next;
            }
            free(cur);
            cur = next;
        }
        else
        {
            prev = cur;
            cur = cur->next;
        }
    }
    return head;
}
struct ListNode* removeElements(struct ListNode* head, int val)
{
    if(head == NULL)
    {
        return head;
    }
    struct ListNode* cur = head;
    struct ListNode* newhead, *tail;
    newhead = tail = NULL;
    while(cur)
    {
        if(cur->val != val)
        {
            if(tail == NULL)
            {
                newhead = tail = cur;
            }
            else
            {
                tail->next = cur;
                tail = tail->next;
            }
            cur = cur->next;
        }
        else
        {
            struct ListNode* next = cur->next;
            free(cur);
            cur = next;
        }   
    }
    if(tail)
        tail->next = NULL;
    return newhead;
}
#define _CRT_SECURE_NO_WARNINGS 1
// 移除单链表中所有值为val的元素
#include<stdio.h>
#include<stdlib.h>
typedef struct SList
{
	int val;
	struct SList* next;
}SLT;

SLT* CreatSLT(int* arr, int z)
{
	SLT* head = NULL;
	SLT* tail = NULL;
	int x = 0;
	for (int i = 0; i < z; i++)
	{
		SLT* newnode = (SLT*)malloc(sizeof(SLT));
		if (newnode == NULL)
		{
			perror("malloc fail");
			exit(-1);
		}
		else
		{
			newnode->val = *(arr + i);
			newnode->next = NULL;
		}
		if (head == NULL)
		{
			head = tail =  newnode;
		}
		else
		{
			tail->next = newnode;
			tail = tail->next;
		}
	}
	return head;

}

SLT* removeElements(SLT* head, int val) {
    if (head == NULL)
        return NULL;
    SLT* cur = head;
	SLT* prev = NULL;
    while (cur)
    {
        //如果当前节点是需要删除的节点
        if (cur->val == val)
        {
            //首先保存下一个节点
			SLT* next = cur->next;
            //如果删除的为头节点,更新头节点
            //否则让当前节点的前趋节点链接next节点
            if (prev == NULL)
            {
                head = cur->next;
            }
            else
            {
                prev->next = cur->next;
            }
            //释放当前节点,让cur指向next
            free(cur);
            cur = next;
        }
        else
        {
            //如果cur不是需要删除的节点,则更新prev,cur
            prev = cur;
            cur = cur->next;
        }
    }

    return head;
}

void SLTPrint(SLT* phead)
{
	SLT* cur = phead;
	while (cur)
	{
		printf("%d->", cur->val);
		cur = cur->next;
	}
	printf("NULL\n");
}
int main()
{
	int arr[] = {1, 2, 6, 3, 4, 5, 6};
	int val = 4;
	SLT* plist = CreatSLT(arr, sizeof(arr)/sizeof(arr[0]));
	SLTPrint(plist);
	SLT* result = removeElements(plist, val);
	SLTPrint(result);
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值