题目链接:https://leetcode-cn.com/problems/design-linked-list/
题目描述
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。
在链表类中实现这些功能:
- get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
- addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
- addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
- addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
- deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
实现
#include<iostream>
using namespace std;
class MyLinkedList {
public:
struct LinkedNode{
int val;
LinkedNode *next;
LinkedNode(int x) : val(x), next(nullptr) {}
};
LinkedNode *vhead; //定义虚拟头节点
int size; //链表长度,这里定义成全局变量
/** Initialize your data structure here. */
MyLinkedList() {
vhead = new LinkedNode(0);
size=0;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
if(index>(size-1)||index<0){
return -1;
}
LinkedNode *node = vhead->next;
while(index--){
node = node->next;
}
return node->val;
}
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
void addAtHead(int val) {
LinkedNode* node = new LinkedNode(val);
node->next = vhead->next;
vhead->next = node;
size++;
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
LinkedNode* node = new LinkedNode(val);
LinkedNode* cur = vhead;
while(cur->next!=nullptr){
cur = cur->next;
}
cur->next = node;
size++;
}
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
void addAtIndex(int index, int val) {
if(index>size){
return;
}
LinkedNode* node = new LinkedNode(val);
LinkedNode* cur = vhead;
while(index--){
cur = cur->next;
}
node->next = cur->next;
cur->next = node;
size++;
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
if(index>=size||index<0){
return;
}
LinkedNode* cur = vhead;
while(index--){
cur = cur->next;
}
LinkedNode *tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
size--;
}
};
int main(){
MyLinkedList *linkedList = new MyLinkedList();
linkedList->addAtHead(1);
linkedList->addAtTail(3);
linkedList->addAtIndex(1,2); //链表变为1-> 2-> 3
linkedList->get(1); //返回2
linkedList->deleteAtIndex(1); //现在链表是1-> 3
cout<<linkedList->get(1);
}
几个需要注意的地方:
delete不能直接删除cur->next否则会出问题,直接陷入死循环
注意操作是否越界,否则报错
反转链表
题目地址:https://leetcode-cn.com/problems/reverse-linked-list/
给你单链表的头节点 head
,请你反转链表,并返回反转后的链表。
输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]
输入:head = [1,2]
输出:[2,1]
输入:head = []
输出:[]
思路
首先定义一个cur指针,指向头结点,再定义一个pre指针,初始化为null。
然后就要开始反转了,首先要把 cur->next 节点用tmp指针保存一下,也就是保存一下这个节点。
为什么要保存一下这个节点呢,因为接下来要改变 cur->next 的指向了,将cur->next 指向pre ,此时已经反转了第一个节点了。
接下来,继续移动pre和cur指针,pre总是指向cur的前一个元素(未反转的情况下)。
最后,cur 指针已经指向了null,循环结束,链表也反转完毕了。 此时我们return pre指针就可以了,pre指针就指向了新的头结点。
代码实现
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *cur = head;
ListNode *pre = NULL; //前驱
ListNode *tmp;
while(cur!=nullptr){
tmp = cur->next;
cur->next = pre; //cur的next指针指向pre
pre = cur; //pre后移
cur = tmp; //cur后移
}
return pre;
}
};