[刷题] LeetCode 237 Delete Nodes in a Linked List
Posted cxc1357
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[刷题] LeetCode 237 Delete Nodes in a Linked List相关的知识,希望对你有一定的参考价值。
要求
- 给定链表中的一个节点,删除该节点
思路
- 通过改变节点的值实现
实现
1 struct ListNode { 2 int val; 3 ListNode *next; 4 ListNode(int x) : val(x), next(NULL) {} 5 }; 6 7 class Solution { 8 public: 9 void deleteNode(ListNode* node) { 10 11 if( node == NULL ) 12 return; 13 14 if( node->next == NULL){ 15 delete node; 16 node = NULL; 17 return; 18 } 19 20 node->val = node->next->val; 21 ListNode* delNode = node->next; 22 node->next = delNode->next; 23 24 delete delNode; 25 26 return; 27 } 28 };
以上是关于[刷题] LeetCode 237 Delete Nodes in a Linked List的主要内容,如果未能解决你的问题,请参考以下文章
[Leetcode]237. Delete Node in a Linked List
LeetCode笔记:237. Delete Node in a Linked List
[LeetCode]237. Delete Node in a Linked List
Leetcode刷题100天—237. 删除链表中的节点(链表)—day01