写在前面:链表不熟,所以多做点题并总结。
不全。
/**
* 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 *dummy=new ListNode(0);
dummy->next=head;
ListNode *cur=dummy;
while(cur->next!=NULL)
{
if(cur->next->val==val)
{
ListNode *temp=cur->next;
cur->next=cur->next->next;//跳过它
delete temp;//删掉它
}
else
{
cur=cur->next;//留下它并往下走
}
}
head=dummy->next;
return head;
}
};
class MyLinkedList {
public:
struct LinkedNode
{
int val;
LinkedNode* next;
LinkedNode(int val):val(val),next(nullptr){}
};
MyLinkedList() {
dummy=new LinkedNode(0);
s=0;//size
}
int get(int index) {
if(index>s-1||index<0)
{
return -1;
}
LinkedNode *cur=dummy->next;
while(index--)
{
cur=cur->next;
}
return cur->val;
}
void addAtHead(int val) {
LinkedNode *temp=new LinkedNode(val);
temp->next=dummy->next;
dummy->next=temp;
s++;
}
void addAtTail(int val) {
LinkedNode *tail=new LinkedNode(val);
LinkedNode *temp=dummy;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=tail;
s++;
}
void addAtIndex(int index, int val) {
LinkedNode *cur=new LinkedNode(val);
LinkedNode *temp=dummy;
if(index<=0)
{
cur->next=temp->next;
dummy->next=cur;
s++;
}
else if(index<=s)
{
while(index--)
{
temp=temp->next;
}
cur->next=temp->next;
temp->next=cur;
s++;
}
}
void deleteAtIndex(int index) {
if(index>=0&&index<s)
{
LinkedNode *temp=dummy;
while(index--)
{
temp=temp->next;
}
temp->next=temp->next->next;
s--;
}
}
private:
LinkedNode *dummy;
int s;
};
/**
* 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);
*/
/**
* 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 *pre=NULL;
ListNode *cur=head;
if(head==NULL) return NULL;
ListNode *temp=head->next;
while(cur!=NULL)
{
cur->next=pre;
pre=cur;
cur=temp;
if(temp!=NULL)temp=temp->next;
}
return pre;
}
};
24. 两两交换链表中的节点
这是一道很好的理解链表相关操作的题。
/**
* 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* swapPairs(ListNode* head) {
ListNode *dummy=new ListNode(0,head);
ListNode *temp=dummy;
while(temp!=NULL)
{
ListNode *a=temp->next;
if(a==NULL) break;
ListNode *b=a->next;
if(b==NULL) break;
temp->next=b;
a->next=b->next;
b->next=a;
temp=a;//要往前移动两格
}
return dummy->next;
}
};
/**
* 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* removeNthFromEnd(ListNode* head, int n) {
ListNode *dummy=new ListNode(0);
dummy->next=head;
ListNode *a=dummy,*b=dummy;
for(int i=0;i<=n;i++) a=a->next;
while(a!=NULL)
{
a=a->next;
b=b->next;
}
b->next=b->next->next;
return dummy->next;
}
};