LeetCode : Remove Linked List Elements
题目原意:Remove all elements from a linked list of integers that have value val.
Example
Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6
Return: 1 –> 2 –> 3 –> 4 –> 5
代码如下(leetCode 测得运行时间为12ms):
题目原意:Remove all elements from a linked list of integers that have value val.
Example
Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6
Return: 1 –> 2 –> 3 –> 4 –> 5
代码如下(leetCode 测得运行时间为12ms):
struct ListNode *removeElements(struct ListNode *head, int val)
{
struct ListNode *pNow = NULL;
while (head && head->val == val) //!< 排除head->val == val
{
head = head->next;
}
if (head == NULL)
{
return NULL;
}
pNow = head;
while (pNow->next) //!< 循环依次排除
{
if (pNow->next->val == val)
{
pNow->next = pNow->next->next;
continue;
}
pNow = pNow->next;
}
return head;
}
本文介绍了解决LeetCode上的移除链表元素问题的方法。该问题是要求从整数链表中移除所有值为特定数值的元素。提供了一个C语言实现的解决方案,其运行时间为12ms。
1874

被折叠的 条评论
为什么被折叠?



