链表part1
LeetCode203
这一题我们要注意如果要移除一个节点,我们可能要手动释放其中的节点。还有就是删除一个节点,我们需要得到这个节点的前一个节点。
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int x) :val(x), next(NULL) {}
};
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummyHead = new ListNode(0); // 设置一个虚拟头结点
dummyHead->next = head; // 将虚拟头结点指向head,这样方便后面做删除操作
ListNode* cur = dummyHead;
while (cur->next != NULL) {
if (cur->next->val == val) {
ListNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
}
else {
cur = cur->next;
}
}
head = dummyHead->next;
delete dummyHead;
return head;
}
int main(){
int num;
cin >> num;
ListNode* head = new ListNode(num);//创建链表头节点
ListNode* ptr = head;//一般不会在原链表上直接操作,创建指向head的ptr,对ptr进行操作
while (cin.get() != '\n'){
cin >> num;
ptr->next = new ListNode(num);
ptr = ptr->next;
}
//while (head) {//遍历输出,最后可以删除
// if (head->next == NULL)
// cout << head->val << "-> NULL";
// else
// cout << head->val << "->";
// head = head->next;
//}
int val;
cin>>val;
removeElements(head, val);
cout << "删除链表中给定值val后:" << endl;
while (head) {//遍历输出,最后可以删除
if (head->next == NULL)
cout << head->val << "-> NULL";
else
cout << head->val << "->";
head = head->next;
}
return 0;
}
LeetCode707题
这一题通过设置虚拟头节点的方式比较好。
class MyLinkedList {
public:
// 定义链表节点结构体
struct LinkedNode {
int val;
LinkedNode* next;
LinkedNode(int val) :val(val), next(nullptr) {}
};
// 初始化链表
MyLinkedList() {
_dummyHead = new LinkedNode(0); // 这里定义的头结点 是一个虚拟头结点,而不是真正的链表头结点
_size = 0;
}
// 获取到第index个节点数值,如果index是非法数值直接返回-1, 注意index是从0开始的,第0个节点就是头结点
int get(int index) {
if (index > (_size - 1) || index < 0) {
return -1;
}
LinkedNode* cur = _dummyHead->next;
while (index--) { // 如果--index 就会陷入死循环
cur = cur->next;
}
return cur->val;
}
// 在链表最前面插入一个节点,插入完成后,新插入的节点为链表的新的头结点
void addAtHead(int val) {
LinkedNode* newNode = new LinkedNode(val);
newNode->next = _dummyHead->next;
_dummyHead->next = newNode;
_size++;
}
// 在链表最后面添加一个节点
void addAtTail(int val) {
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead;
while (cur->next != nullptr) {
cur = cur->next;
}
cur->next = newNode;
_size++;
}
// 在第index个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。
// 如果index 等于链表的长度,则说明是新插入的节点为链表的尾结点
// 如果index大于链表的长度,则返回空
// 如果index小于0,则在头部插入节点
void addAtIndex(int index, int val) {
if (index > _size) return;
if (index < 0) index = 0;
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead;
while (index--) {
cur = cur->next;
}
newNode->next = cur->next;
cur->next = newNode;
_size++;
}
// 删除第index个节点,如果index 大于等于链表的长度,直接return,注意index是从0开始的
void deleteAtIndex(int index) {
if (index >= _size || index < 0) {
return;
}
LinkedNode* cur = _dummyHead;
while (index--) {
cur = cur->next;
}
LinkedNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
//delete命令指示释放了tmp指针原本所指的那部分内存,
//被delete后的指针tmp的值(地址)并非就是NULL,而是随机值。也就是被delete后,
//如果不再加上一句tmp=nullptr,tmp会成为乱指的野指针
//如果之后的程序不小心使用了tmp,会指向难以预想的内存空间
tmp = nullptr;
_size--;
}
// 打印链表
void printLinkedList() {
LinkedNode* cur = _dummyHead;
while (cur->next != nullptr) {
cout << cur->next->val << " ";
cur = cur->next;
}
cout << endl;
}
private:
int _size;
LinkedNode* _dummyHead;
};
LeetCode206反转链表
这一题用双指针法可以解决。
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int x) :val(x),next(NULL) {}
};
ListNode* reverseList(ListNode* head) {
ListNode* temp; // 保存cur的下一个节点
ListNode* cur = head;
ListNode* pre = NULL;
while (cur) {
temp = cur->next; // 保存一下 cur的下一个节点,因为接下来要改变cur->next
cur->next = pre; // 翻转操作
// 更新pre 和 cur指针
pre = cur;
cur = temp;
}
return pre;
}
void printListNode(ListNode* head) {
ListNode* cur = head;
while (cur != nullptr) {
cout << cur->val << " ";
cur = cur->next;
}
cout << endl;
}
int main(){
int n, m;
ListNode* dummyHead = new ListNode(0);
while (cin>>n) {
if (n == 0) {
cout << "list is empty" << endl;
continue;
}
ListNode* cur = dummyHead;
//读取输入构建列表
while (n--) {
cin >> m;
ListNode* newNode = new ListNode(m);
cur->next = newNode;
cur = cur->next;
}
cout << "反转链表前:" << endl;
printListNode(dummyHead->next);
cout << "反转链表后:" << endl;
printListNode(reverseList(dummyHead->next));
}
return 0;
}