-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28_basic.cpp
More file actions
59 lines (52 loc) · 1.23 KB
/
28_basic.cpp
File metadata and controls
59 lines (52 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
void deleteNode(ListNode *node)
{
*(node) = *(node->next);
}
};
int main()
{
Solution p;
ListNode q = ListNode(1); // 1->4->6->8
cout << q.val << " " << q.next << endl;
ListNode *head, *point;
ListNode x0 = ListNode(-1),
x1 = ListNode(1),
x2 = ListNode(4),
x3 = ListNode(6),
x4 = ListNode(8); // -1-->1->4->6->8
// head = &ListNode(-1); 不可行,右边是临时变量,地址不可用
head = &x0;
point = head;
head->next = &x1;
head->next->next = &x2;
head->next->next->next = &x3;
head->next->next->next->next = &x4;
while (point != NULL)
{
cout << point->val << "->";
point = point->next;
} // 打印出原链表
cout << endl;
p.deleteNode(head->next->next); // delete x2 = 4
point = head;
while (point != NULL)
{
cout << point->val << "->";
point = point->next;
}
cout << endl;
return 0;
}