题目:206. 反转链表 - 力扣(LeetCode)
/**
* 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) {
if(head==nullptr||head->next==nullptr)
{
return head;
}
//双指针法
ListNode* prev=head;
ListNode* cur=head->next;
ListNode* next=cur->next;
head->next=nullptr;
while(cur!=nullptr)
{
next=cur->next;
cur->next=prev;
prev=cur;
cur=next;
}
return prev;
}
};
题目203. 移除链表元素 - 力扣(LeetCode)
/**
* 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* removeElements(ListNode* head, int val) {
ListNode* newHead=new ListNode(0);
newHead->next=head;
ListNode* cur=newHead;
while(cur!=nullptr&&cur->next!=nullptr)
{
if(cur->next->val==val)
{
ListNode*tmp=cur->next;
cur->next=cur->next->next;
delete tmp;
}else{
cur=cur->next;
}
}
return newHead->next;
}
};
题目707. 设计链表 - 力扣(LeetCode)
class MyLinkedList {
public:
// 节点定义
struct MyLinkedNode {
int val;
MyLinkedNode* next;
MyLinkedNode() {
val = 0;
next = nullptr;
}
};
public:
MyLinkedList() {
head = new MyLinkedNode();
size = 0;
}
int get(int index) {
if (index > size - 1 || index < 0) {
return -1;
}
MyLinkedNode* cur = head;
while (index-- >= 0) {
cur = cur->next;
}
if (cur != nullptr) {
return cur->val;
} else {
return -1;
}
}
void addAtHead(int val) {
MyLinkedNode* node = new MyLinkedNode();
node->val = val;
node->next = head->next;
head->next = node;
size++;
}
void addAtTail(int val) {
MyLinkedNode* node = new MyLinkedNode();
node->val = val;
node->next = nullptr;
MyLinkedNode* tail = head;
while (tail->next != nullptr) {
tail = tail->next;
}
tail->next = node;
size++;
}
void addAtIndex(int index, int val) {
if (index == size) {
addAtTail(val);
return;
} else if (get(index) == -1) // index不合规
{
return;
}
MyLinkedNode* node = head;
while (index-- > 0) {
node = node->next;
}
MyLinkedNode* newNode = new MyLinkedNode();
newNode->val = val;
if (node->next == nullptr) {
newNode->next = nullptr;
} else {
newNode->next = node->next;
}
node->next = newNode;
size++;
return;
}
void deleteAtIndex(int index) {
if (get(index) == -1) // index不合规
{
return;
}
MyLinkedNode* node = head;
while (index-- > 0) {
node = node->next;
}
MyLinkedNode* tmp = node->next;
if (tmp != nullptr) {
node->next = tmp->next;
}
size--;
return;
}
private:
MyLinkedNode* head;
int size;
};
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList* obj = new MyLinkedList();
* int param_1 = obj->get(index);
* obj->addAtHead(val);
* obj->addAtTail(val);
* obj->addAtIndex(index,val);
* obj->deleteAtIndex(index);
*/
最后
注意节点指针是否为空,反转链表有两种方法,双指针法和递归法,递归法继续看看,双指针法要熟记,注意链表循环结束条件,,可以边画图边写代码,提高准确率,,注意链表可以采用哨兵节点也就是虚拟节点来快速破题