LeetCode 剑指 Offer 18. 删除链表的节点
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * struct ListNode *next; 6 * }; 7 */ 8 9 10 struct ListNode* deleteNode(struct ListNode* head, int val){ 11 struct ListNode* l, *h, *prev; 12 13 if (head == NULL) 14 return NULL; 15 if (head->val == val) { 16 l = head->next; 17 head->next = NULL; 18 return l; 19 } 20 h = head; 21 prev = head; 22 l = head->next; 23 while (l){ 24 if (l->val == val) { 25 prev->next = l->next; 26 l->next = NULL; 27 break; 28 } 29 prev = l; 30 l = l->next; 31 } 32 return h; 33 }