File tree Expand file tree Collapse file tree 3 files changed +100
-0
lines changed
algorithms/DeleteNodeinaLinkedList Expand file tree Collapse file tree 3 files changed +100
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments