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

目录

203.移除链表元素

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

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

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

206.反转链表

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

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

[876. 链表的中间结点](#876. 链表的中间结点)


203.移除链表元素

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

cpp 复制代码
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的节点拿出来进行尾插到新链表

cpp 复制代码
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;
}

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

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

cpp 复制代码
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.反转链表

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

cpp 复制代码
 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;
}

方法二:将指针方向颠倒

cpp 复制代码
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; 是停止。

cpp 复制代码
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;
}
相关推荐
患得患失9496 小时前
【算法】力扣体系分类
算法·leetcode·分类
asom2213 小时前
LeetCode Hot100(字串)
算法·leetcode
2301_7944615715 小时前
力扣-将x减到0的最小操作数
数据结构·算法·leetcode
星沁城16 小时前
108. 将有序数组转换为二叉搜索树
java·数据结构·leetcode
姬公子52119 小时前
leetcode hot100刷题日记——9.矩阵置零
算法·leetcode·矩阵
十年一觉尘与土19 小时前
74. 搜索二维矩阵
leetcode
Cherl.19 小时前
C语言 贪吃蛇小游戏的实现
c语言·开发语言·链表·贪吃蛇
不知道写什么的作者1 天前
今日打卡,Leetcode第四题:寻找两个正序数组的中位数,博主表示就会sorted
算法·leetcode·职场和发展
顽强卖力1 天前
python之数据结构与算法篇
数据结构·python·算法·链表·排序算法·哈希算法
沐风ya1 天前
leetcode每日一题(昨天的) -- 3356. 零数组变换 II
数据结构·算法·leetcode