Skip to content

Commit 1263e10

Browse files
committed
237 Delete Node in a Linked List
1 parent aa8451c commit 1263e10

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
+ [230 Kth Smallest Element in a BST(BST、中序遍历)](algorithms/KthSmallestElementinaBST)
130130
+ [231 Power of Two(位运算,二进制1的个数)](algorithms/PowerofTwo)
131131
+ [232 Implement Queue using Stacks(栈模拟队列)](algorithms/ImplementQueueusingStacks)
132+
+ [237 Delete Node in a Linked List(O(1)删除单链表节点)](algorithms/DeleteNodeinaLinkedList)
132133

133134
## Database
134135

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Delete Node in a Linked List
2+
3+
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
4+
5+
Supposed the linked list is `1 -> 2 -> 3 -> 4` and you are given the third node with value `3`, the linked list should become `1 -> 2 -> 4` after calling your function.
6+
7+
## Solution
8+
9+
给定一个单链表节点,删除该节点。
10+
11+
由于无法获得该节点的前驱节点,因此无法更新前驱节点的后继节点。我们可以把该节点的下一个节点的值拷贝到当前节点,转而删除该节点的下一个节点。
12+
13+
该题目也可以描述为: 如何在O(1)的时间删除单链表的某个节点!
14+
15+
```cpp
16+
void deleteNode(ListNode *node) {
17+
ListNode *p = node->next;
18+
node->val = p->val;
19+
node->next = p->next;
20+
delete p;
21+
}
22+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <string>
6+
using namespace std;
7+
#include <stdlib.h>
8+
struct ListNode {
9+
int val;
10+
ListNode *next;
11+
ListNode(int x): val(x), next(nullptr) {}
12+
};
13+
class Solution {
14+
public:
15+
void deleteNode(ListNode *node) {
16+
ListNode *p = node->next;
17+
node->val = p->val;
18+
node->next = p->next;
19+
delete p;
20+
}
21+
22+
};
23+
int getLength(ListNode *head)
24+
{
25+
int len = 0;
26+
ListNode *p = head;
27+
while (p) {
28+
++len;
29+
p = p->next;
30+
}
31+
return len;
32+
}
33+
void print(ListNode *head)
34+
{
35+
if (head == nullptr) {
36+
printf("NULL\n");
37+
return;
38+
}
39+
struct ListNode *p = head;
40+
while (p) {
41+
printf("%d ", p->val);
42+
p = p->next;
43+
}
44+
printf("\n");
45+
}
46+
ListNode * mk_list(ListNode **ha, int a[], int n)
47+
{
48+
if (n < 1)
49+
return nullptr;
50+
ListNode *p = new ListNode(a[0]);
51+
*ha = p;
52+
for (int i = 1; i < n; ++i) {
53+
ListNode *q = new ListNode(a[i]);
54+
p->next = q;
55+
p = q;
56+
}
57+
return p;
58+
}
59+
void free_list(struct ListNode *head)
60+
{
61+
struct ListNode *p = head;
62+
while (p) {
63+
struct ListNode *q = p->next;
64+
delete p;
65+
p = q;
66+
}
67+
}
68+
int main(int argc, char **argv)
69+
{
70+
Solution solution;
71+
struct ListNode *head = NULL;
72+
int a[] = {1, 2, 3};
73+
mk_list(&head, a, 3);
74+
solution.deleteNode(head->next);
75+
print(head);
76+
return 0;
77+
}

0 commit comments

Comments
 (0)