目录
解法二:遍历原链表,把不是val的节点拿出来进行尾插到新链表编辑
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;
}