为什么这么简单还贴出来,在 2. Add Two Numbers中,我已经解释了,不要问我有多菜……
题目:
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
Language C:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeElements(struct ListNode* head, int val) {
struct ListNode *h, *p;// 指向当前节点的上一个节点
//h = (struct ListNode *)malloc(sizeof(struct ListNode));
//h->val = 0;//头结点
h->next = head;// 指向当前节点的上一个节点
p = h;
//q = head;
while(head){
(head->val == val)?(p->next = head->next):(p = p->next);
head = head->next;
// if(head->val == val){ //当前节点为指定元素值的节点
// p->next = head->next; //当前节点的next给其pre
// head = head->next; //继续遍历剩下的节点
// }
// else{
// head = head->next;
// p = p->next;
// }
}
return h->next; //返回调整后的链表
}