上LeetCode后做的第一道题。删除一个链表节点。两个写法。第一个8ms,第二个4ms。先这么写吧。
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ /* solution 1 // The normal one I can think immediately. void deleteNode(struct ListNode* node) { node->val = node->next->val; node->next = node->next->next; } */ // solution 2 , a little better than solution 1. void deleteNode(struct ListNode* node) { *node = *node->next;//I forgot the priority of operator * and operator -> }
时间: 2024-10-30 15:38:38