?LeetCode刷題實(shí)戰(zhàn)237:刪除鏈表中的節(jié)點(diǎn)
Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly.

示例
示例 1:
輸入:head = [4,5,1,9], node = 5
輸出:[4,1,9]
解釋:給定你鏈表中值為 5 的第二個(gè)節(jié)點(diǎn),那么在調(diào)用了你的函數(shù)之后,該鏈表應(yīng)變?yōu)?4 -> 1 -> 9.
示例 2:
輸入:head = [4,5,1,9], node = 1
輸出:[4,5,9]
解釋:給定你鏈表中值為 1 的第三個(gè)節(jié)點(diǎn),那么在調(diào)用了你的函數(shù)之后,該鏈表應(yīng)變?yōu)?4 -> 5 -> 9.
提示:
鏈表至少包含兩個(gè)節(jié)點(diǎn)。
鏈表中所有節(jié)點(diǎn)的值都是唯一的。
給定的節(jié)點(diǎn)為非末尾節(jié)點(diǎn)并且一定是鏈表中的一個(gè)有效節(jié)點(diǎn)。
不要從你的函數(shù)中返回任何結(jié)果。
解題
node的最常見方法是修改之前節(jié)點(diǎn)的next指針,使其指向之后的節(jié)點(diǎn)。



class Solution {
public:
void deleteNode(ListNode* node) {
node->val = node->next->val;
node->next = node->next->next;
}
};
LeetCode1-220題匯總,希望對你有點(diǎn)幫助!
LeetCode刷題實(shí)戰(zhàn)221:最大正方形
LeetCode刷題實(shí)戰(zhàn)222:完全二叉樹的節(jié)點(diǎn)個(gè)數(shù)
LeetCode刷題實(shí)戰(zhàn)223:矩形面積
LeetCode刷題實(shí)戰(zhàn)224:基本計(jì)算器
LeetCode刷題實(shí)戰(zhàn)225:用隊(duì)列實(shí)現(xiàn)棧
LeetCode刷題實(shí)戰(zhàn)226:翻轉(zhuǎn)二叉樹
LeetCode刷題實(shí)戰(zhàn)227:基本計(jì)算器 II
LeetCode刷題實(shí)戰(zhàn)228:匯總區(qū)間
LeetCode刷題實(shí)戰(zhàn)229:求眾數(shù) II
LeetCode刷題實(shí)戰(zhàn)230:二叉搜索樹中第K小的元素
LeetCode刷題實(shí)戰(zhàn)231:2的冪
LeetCode刷題實(shí)戰(zhàn)232:用棧實(shí)現(xiàn)隊(duì)列
LeetCode刷題實(shí)戰(zhàn)233:數(shù)字 1 的個(gè)數(shù)
LeetCode刷題實(shí)戰(zhàn)234:回文鏈表
LeetCode刷題實(shí)戰(zhàn)235:二叉搜索樹的最近公共祖先
LeetCode刷題實(shí)戰(zhàn)236:二叉樹的最近公共祖先
