# 链表练习题-1

## 移除链表元素

tail的值为NULL,就是循环停止的时候

/**
* struct ListNode {
*     int val;
*     struct ListNode *next;
* };
*/
struct ListNode* removeElements(struct ListNode* head, int val)
{
struct ListNode* prev = NULL;
//遍历一遍
while (cur)
{
if (cur->val == val)
{
struct ListNode* nex = cur->next;
if (prev)
{
prev->next = nex;
}
//判断是否要删除第一个节点
else
{
}

free(cur);
cur = nex;

}
else
{
prev = cur;
cur = cur->next;
}
}
}


struct ListNode* removeElements(struct ListNode* head, int val)
{
struct ListNode* newnode = NULL;
struct ListNode* tail = NULL;
while (cur)
{
if (cur->val != val)
{
if (tail == NULL)
{
newnode = cur;
tail = cur;
}
else
{
tail->next = cur;
tail = cur;

}
cur = cur->next;
}
else
{
//保存下一个节点
struct ListNode* p = cur->next;
free(cur);
cur = p;
}
}
if (tail)
tail->next = NULL;
return newnode;
}


struct ListNode* removeElements(struct ListNode* head, int val)
{
//创建哨兵位
struct ListNode* newnode = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* tail = newnode;
while (cur)
{
if (cur->val != val)
{
tail->next = cur;
cur = cur->next;
tail = tail->next;
}
else
{
//保存下一个节点
struct ListNode* p = cur->next;
free(cur);
cur = p;
}
}
tail->next = NULL;
//释放哨兵
struct ListNode*p = newnode->next;
free(newnode);
return p;
}



## 分割链表

 ListNode* partition(ListNode* pHead, int x)
{
//小
struct ListNode *head1 = (struct ListNode *)malloc(sizeof(struct ListNode));
//大
struct ListNode *head2 = (struct ListNode *)malloc(sizeof(struct ListNode));
while (cur)
{
if(cur->val < x)
{
tail1->next = cur;
tail1 = tail1->next;
}
else
{
tail2->next = cur;
tail2 = tail2->next;
}
cur = cur->next;

}
tail2->next = NULL;
return ph;

}


## 反向链表

struct ListNode* reverseList(struct ListNode* head)
{
struct ListNode *n1 = NULL;
while(n2)
{
n2->next = n1;
n1 = n2;
n2 = n3;
if(n3)
n3 = n3->next;
}
return n1;
}


struct ListNode* reverseList(struct ListNode* head)
{
struct ListNode* newnode = NULL;
while(cur)
{
struct ListNode* pn = cur->next;
cur->next = newnode;
newnode = cur;
cur = pn;
}
return newnode;
}


## 链表中倒数第k个结点

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
if (tail) {
int size = 1;
while (tail->next) {
tail = tail->next;
size++;

}
if (k > size) {
return NULL;
}
while (size > k) {
tail = tail->next;
size--;
}

return tail;
}
else {
return NULL;
}
}


struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
//fast先走k步
while(k--)
{
//防止为空
if(fast ==NULL)
return NULL;
fast = fast->next;
}
//一起走
while (fast)
{
fast = fast->next;
slow = slow->next;
}
return slow;
}


https://developer.aliyun.com/article/1498107

|
2月前
|

20 0
|
2月前
|

28 0
|
2月前
【链表之练习题】
【链表之练习题】
31 1
|
2月前
【无头双向链表和链表练习题2】
【无头双向链表和链表练习题2】
27 0
|
9月前
|

37 1
|
2月前
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解
30 0
|
1月前
|

LeetCode力扣第114题：多种算法实现 将二叉树展开为链表
LeetCode力扣第114题：多种算法实现 将二叉树展开为链表
18 0
|
1月前
|

LeetCode 题目 86：分隔链表
LeetCode 题目 86：分隔链表
21 2
|
1月前
|

【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
14 2
|
2月前
＜数据结构＞五道LeetCode链表题分析.环形链表，反转链表，合并链表，找中间节点.
＜数据结构＞五道LeetCode链表题分析.环形链表，反转链表，合并链表，找中间节点
30 1