首页 > 其他 > 详细

[LeetCode] Delete Node in a Linked List

时间:2015-07-15 14:39:15      阅读:146      评论:0      收藏:0      [点我收藏+]

Well, this problem is just a trick. In fact, we cannot really delete the given node, but just delete its next node after copying the data of the next node to it.

The code is as follows.

1 class Solution {
2 public:
3     void deleteNode(ListNode* node) {
4         ListNode* next = node -> next;
5         node -> val = next -> val;
6         node -> next = next -> next;
7     }
8 };

If you are familiar with pointer operations, the code can be even shorter, just 1-line!

1 class Solution {
2 public:
3     void deleteNode(ListNode* node) {
4         *node = *node -> next;
5     }
6 };

This link shares solutions to this problem in all supported languages of the LeetCode OJ. You may take a look at it and appreciate the appetite of each language.

[LeetCode] Delete Node in a Linked List

原文:http://www.cnblogs.com/jcliBlogger/p/4648091.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!