leetcode(力扣):203移除链表元素 leetcode(力扣):206反转链表 leetcode(力扣):876.链表的中间结点多种解法

目录

203.移除链表元素

解法一:将目标元素前一个元素存放地址改为下一元素地址

解法二:遍历原链表,把不是val的节点拿出来进行尾插到新链表​编辑

解法三:有哨兵位解法->头节点不存储有效数据​编辑

206.反转链表

方法一:创建新指针​编辑方法一:创建新指针进行反转​编辑

方法二:将指针方向颠倒​编辑

876. 链表的中间结点


203.移除链表元素

解法一:将目标元素前一个元素存放地址改为下一元素地址

struct ListNode* removeElements(struct ListNode* head, int val)
{
	struct ListNode* prev = NULL;
	struct ListNode* cur = head;

	if (head == NULL)//检验链表是否为空
	{
		return head;
	}

	while (cur)
	{
		if (cur->val == val)
		{
			if (cur == head)//if(prev==NULL) 
			{
				//头删->以防该数组组成相同倒是prev一直为NULL导致error
				head = cur->next;
				free(cur);
				cur = head;
			}
			else
			{
				//删除
				prev->next = cur->next;//将cur上一个元素的指针存放的地址改为cur存放的地址即cur下一个元素的地址
				free(cur);//释放cur
				cur = prev->next;//使cur指向prev的下一个元素
			}
		}
		else
		{
			prev = cur;
			cur = cur->next;
		}
	}
	return head;
}

解法二:遍历原链表,把不是val的节点拿出来进行尾插到新链表

struct ListNode* removeElements(struct ListNode* head, int val)
{
	struct ListNode* cur = head;
	struct ListNode* tail = NULL;
	head = NULL;//防止野指针

	while (cur)
	{
		if (cur->val == val)
		{
			//删除
			struct ListNode* del = cur;
			cur = cur->next;
			free(del);
		}
		else
		{
			//尾插->第一步
			if (tail==NULL)
			{
				head = tail = cur;
			}
			else
			{
				tail->next = cur;
				tail = tail->next;
			}
			cur = cur->next;
		}
	}
	if(tail!=NULL)
		tail->next = NULL;//防止野指针

	return head;
}

解法三:有哨兵位解法->头节点不存储有效数据

该解法好处在于无需考虑第一步尾插

struct ListNode* removeElements(struct ListNode* head, int val)
{
	struct ListNode* cur = head;
	struct ListNode* tail = NULL;

	//哨兵位的头节点
	head = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
	tail->next = NULL;

	while (cur)
	{
		if (cur->val == val)
		{
			//删除
			struct ListNode* del = cur;
			cur = cur->next;
			free(del);
		}
		else
		{
			tail->next = cur;
			tail = tail->next;
			cur = cur->next;
		}
	}
	tail->next=NULL;

	//释放哨兵位
	struct ListNode* del = head;
	head = head->next;
	free(del);

	return head;
}

206.反转链表

方法一:创建新指针方法一:创建新指针进行反转

 struct ListNode* reverseList(struct ListNode* head)
{
    struct ListNode* newhead = NULL;
    struct ListNode* cur = head;

    while (cur)
    {
        //存放cur下一节点地址
        struct ListNode* next = cur->next;

        //头插
        cur->next = newhead;//将前一个节点地址存放在当前cur中
        newhead = cur;//nwehead后移

        cur = next;//原链表指针后移一位
    }
    return newhead;
}

方法二:将指针方向颠倒

struct ListNode* reverseList(struct ListNode* head)
{
   //防止链表为空
   if (head == NULL)
   {
       return NULL;
   }

   struct ListNode* n1, * n2, * n3;
   n1 = NULL;
   n2 = head;
   n3 = n2->next;

   while (n2)
   {
       //倒指向
       n2->next = n1;

       //迭代
       n1 = n2;
       n2 = n3;

       //防止n3越界访问
       if (n3)
           n3 = n3->next;
   }
   //最后一步时,n1=n2处于反转后首元素位置,故直接return n1
   return n1;
}

876. 链表的中间结点


构建两个指针:快慢指针slow,fast
slow一次走一步,fast一次走两步,当fast走到尾的时候slow就走到了中间。
偶数有两个中间节点,题目要求返回第二个->fast==NULL; 是停止。

struct ListNode* middleNode(struct ListNode* head)
{
    struct ListNode* slow, * fast;
    slow = fast = head;

    //fast:检验奇数,fast->next:检验偶数
    while (fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}
  • 20
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值