LeetCode Daily Question 26

5 月 9 日 每日一题


题目:237. Delete Node in a Linked List

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.

It is guaranteed that the node to be deleted is not a tail node in the list.

思路

一道 easy 题,先将下一个 node 的信息保存到该 node,然后删除下一 node。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
void deleteNode(ListNode* node) {
node->val = node->next->val;
ListNode* forwarding = node->next->next;
delete node->next;
node->next = forwarding;
}
};

  1. 5 月 9 日 每日一题
    1. 题目:237. Delete Node in a Linked List
    2. 思路
    3. 代码