1.移除元素 特殊节点的处理需要记住
class Solution
{
public:
ListNode* removeElements(ListNode* head,int val){
if(head==nullptr){
return nullptr;
}
//如果头节点满足 需要特殊处理
while (head->val==val)
{
ListNode* tmp=head;
head=head->next;
delete tmp;
tmp=nullptr;
}
ListNode* cur=head;
//不要设置快慢指针 直接用cur->next去判断
while (cur!=nullptr&&cur->next!=nullptr)
{
/* code */
if (cur->next->val==val)
{
ListNode *tmp=cur->next;
cur->next=cur->next->next;
delete tmp;
tmp=nullptr;
continue;
}
cur=cur->next;
}
return head;
}
};
2.设计链表 看看得了
class MyLinkedList
{
public:
MyLinkedList()
{
this->size = 0;
this->head = new ListNode(0);
}
int get(int index)
{
if (index < 0 || index >= size)
return -1;
ListNode *p = head;
for (int i = 0; i <=index; i++)
{
/* code */
p=p->next;
}
return p->val;
}
void addAtHead(int val)
{
addAtIndex(0, val);
}
void addAtTail(int val)
{
addAtIndex(size, val);
}
void addAtIndex(int index, int val)
{
if (index > size)
{
/* code */
return;
}
index = max(0, index);
size++;
ListNode *pred = head;
for (int i = 0; i < index; i++)
{
/* code */
pred = pred->next;
}
ListNode *toAdd = new ListNode(val);
toAdd->next = pred->next;
pred->next = toAdd;
}
void deleteAtIndex(int index)
{
if (index < 0 || index >= size)
{
/* code */
return;
}
size--;
ListNode *pred = head;
for (int i = 0; i < index; i++)
{
/* code */
pred = pred->next;
}
ListNode *p = pred->next;
pred->next = pred->next->next;
delete p;
p = nullptr;
}
private:
int size;
ListNode *head;
};
3.两两交换节点
class Solution
{
public:
ListNode *swapPairs(ListNode *head)
{
if (head == nullptr)
{
return nullptr;
}
ListNode *cur = head;
ListNode *pre = nullptr;
int flag = 0;
while (cur != nullptr && cur->next != nullptr)
{
flag++;
ListNode *temp = cur->next;
cur->next = cur->next->next;
temp->next = cur;
if (flag == 1)
{
/* code */
head = temp;
}
else
{
pre->next = temp;
}
pre = cur;
cur = cur->next;
}
return head;
}
};
6.删除倒数第N个节点
class Solution
{
public:
ListNode *removeNthFromEnd(ListNode *head, int n)
{
if (head == nullptr)
{
return nullptr;
}
int count = 0;
ListNode *p = head;
ListNode *q = head;
// 先走N步
while (p != nullptr)
{
count++;
p = p->next;
if (count == n)
{
break;
}
}
if (count < n)
{
/* code */
// 表明个数小于n
return head;
}
ListNode *pre = q;
//特殊处理下删的是头结点
if (p==nullptr)
{
return head->next;
}
while (p != nullptr)
{
p = p->next;
pre = q;
q = q->next;
if (p == nullptr)
{
/* code */
pre->next = q->next;
delete q;
q=nullptr;
return head;
}
}
return head;
}
private:
int size;
ListNode *head;
};
7.链表相交
class Solution
{
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB){
int lenA=0,lenB=0;
ListNode *p=headA;
ListNode *curA=headA;
ListNode *curB=headB;
while (p!=nullptr)
{
lenA++;
p=p->next;
}
p=headB;
while (p!=nullptr)
{
lenB++;
p=p->next;
}
//强制A的长一些
if (lenA<lenB)
{
swap(lenA,lenB);
swap(curA,curB);
}
int n=lenA-lenB;
while (n--)
{
/* code */
curA=curA->next;
}
while (curA!=nullptr&&curB!=nullptr)
{
if (curA==curB)
{
return curA;
}
curA=curA->next;
curB=curB->next;
}
return nullptr;
}
};
8.环形链表ii 记住就是快慢指针一定会相遇 相遇完从头开始一步一步走
class Solution
{
public:
ListNode *detectCycle(ListNode *head)
{
// 先判断是否有环
ListNode *fast = head;
ListNode *slow = head;
while (fast != nullptr && fast->next != nullptr)
{
/* code */
//可能快指针转了很多圈
slow = slow->next;
fast = fast->next->next;
// 快慢指针相遇
if (slow == fast)
{
/* code */
ListNode *index1 = fast;
ListNode *index2 = head;
while (index1 != index2)
{
/* code */
index1 = index1->next;
index2 = index2->next;
}
return index2;
}
}
//没有环
return nullptr;
}
};