题目链接:https://leetcode-cn.com/leetbook/read/linked-list/f9izv/
思路并不难:重新建立新的链表
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeElements(struct ListNode* head, int val){
if(head == NULL){
return head;
}
struct ListNode* newHead = (struct ListNode*)malloc(sizeof(struct ListNode));
newHead->next = NULL;
struct ListNode* p = newHead;
while(head){
if(head->val==val){
head = head->next;
continue;
}
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
p->next = newNode;
newNode->next = NULL;
newNode->val = head->val;
p = newNode;
head = head->next;
}
return newHead->next;
}