[leetcode] 237. Delete Node in a Linked List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: void deleteNode(ListNode* node) { if(!node) return; node->val=node->next->val; node->next=node->next->next; } };
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
1. 没有告诉链表首指针,没有前指针,直接给了要删的节点指针,不需要找特定元素值的节点。
2. 把后一节点的值复制到当前节点,然后删掉后一节点。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步