码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Delete Node in a Linked List

时间:2015-07-15 14:41:47      阅读:90      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!