删除单向链表所有的val
struct ListNode* removeElements(struct ListNode* head, int val){
if (head == NULL)
return NULL;
struct ListNode* prev = head;
struct ListNode* cur = head->next;
while (cur != NULL){
if (cur->val = val){
prev->next = cur->next;
free(cur);
cur = prev->next;
}
else{
prev = cur;
cur = cur->next;
}
}
if (head->val == val){
struct Listnode* newHead = head->next;
free(head);
return newHead;
}
else
return head;
}
反转单链表
struct ListNode* reverseList(struct ListNode* head){
struct ListNode* ret = NULL;
while (head != NULL){
struct ListNode* node = head;
head = head->next;
node->next = ret;
ret = node;
}
return ret;
}
struct ListNode* reverseList(struct ListNode* head){
struct ListNode *prev, *cur, *tmp;
prev = NULL;
cur = head;
if(head == NULL)
return head;
while(cur != 0)
{
tmp = cur->next;
cur->next = prev;
prev = cur;
cur = tmp;
}
return prev;
}
给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点
struct ListNode* middleNode(struct ListNode* head){
if (head == NULL)
return NULL;
struct ListNode* fast = head;
struct ListNode* slow = head;
while (1)
{
fast = fast->next;
if (fast == NULL)
break;
slow = slow->next;
fast = fast->next;
if (fast == NULL)
break;
}
return slow;
}
删除链表的倒数第 n 个节点,并且返回链表的头结点。
struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
struct ListNode* fast = fast;
struct ListNode* slow = slow;
fast = slow = head;
for(int count = 0; count < n-1 ;count ++){
fast = fast-> next;
}
if(fast->next == NULL){
struct ListNode* newhead = head->next;
free(head);
return newhead;
}
else{
struct ListNode* oldslow = slow;
while(fast->next != NULL){
oldslow = slow;
fast = fast-> next;
slow = slow->next;
}
oldslow->next = oldslow->next->next;
free(slow);
}
return head;
}
合并有序链表
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
if(l1==NULL)
return l2;
if(l2==NULL)
return l1;
if(l1->val < l2->val){
l1->next = mergeTwoLists(l1->next,l2);
return l1;
}else{
l2->next = mergeTwoLists(l1,l2->next);
return l2;
}
}
链表分割
class Partition {
public:
ListNode* partition(ListNode* pHead, int x) {
ListNode* slist = new ListNode (-1);
ListNode* blist = new ListNode (-1);
ListNode* cur = pHead, *dslist = slist , *dblist = blist;
while(cur != NULL){
if(cur->val < x){
dslist->next = cur;
dslist = cur;
}else{
dblist->next = cur;
dblist = cur;
}
cur = cur->next;
}
dslist->next = blist->next;
dblist->next = 0;
return slist->next;
}
};
删除有序链表中重复的结点(不保留)
ListNode* deleteDuplication(ListNode* pHead)
{
if (pHead == NULL || pHead->next == NULL)
return pHead;
if (pHead->val == pHead->next->val)
{
ListNode *cur = pHead->next;
while (cur != NULL && cur->val == pHead->val)
cur = cur->next;
return deleteDuplication(cur);
}
else
{
pHead->next = deleteDuplication(pHead->next);
return pHead;
}
链表回文
ListNode* middleNode(ListNode* head){
if (head == NULL)
return NULL;
ListNode* slow = slow;
ListNode* fast = fast;
while (1)
{
fast = fast->next;
if (fast == NULL){
break;
}
slow = slow->next;
fast = fast->next;
if (fast == NULL){
break;
}
}
return slow;
}
ListNode* reverseList(ListNode* head){
if (head == NULL)
return NULL;
ListNode *cur, *prev, *next;
prev = NULL;
cur = head;
next = head->next;
while (cur != NULL){
cur->next = prev;
prev = cur;
cur = next;
if (next != NULL)
next = next->next;
}
return prev;
}
bool chkPalindrome(ListNode* A){
ListNode * middle = middleNode(A);
ListNode * r = reverseList(middle->next);
ListNode *n1 = A, *n2 = r;
while (n1 != NULL && n2 != NULL){
if (n1->val != n2->val){
return false;
}
n1 = n1->next;
n2 = n2->next;
}
return true;
}
相交链表
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (!headA || !headB)
return nullptr;
while (headA)
{
ListNode* tmp = headB;
if (headA == tmp)
return headA;
while (tmp->next != nullptr){
tmp = tmp->next;
if (headA == tmp)
return headA;
}
headA = headA->next;
}
return nullptr;
}
};
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (!headA || !headB)
return nullptr;
ListNode* countA = headA;
ListNode* countB = headB;
int lA = 0;
int lB = 0;
while (countA){
++lA;
countA = countA->next;
}
while (countB){
++lB;
countB = countB->next;
}
int i = max(lA, lB) - min(lA, lB);
if (lA > lB){
for (i; i > 0; i--)
headA = headA->next;
}
else{
for (i; i > 0; i--)
headB = headB->next;
}
while (headA == headB) {
headA = headA->next;
headB = headB->next;
}
return headA;
}
};
判断链表是否带环
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
bool isCycle = false;
ListNode* slow = head;
ListNode* fast = head;
while (fast && fast->next)
{
fast = fast->next->next;
slow = slow -> next;
if (fast == slow){
isCycle = true;
break;
}
}
if (!isCycle)
return NULL;
slow = head;
while (fast != slow){
fast = fast->next;
slow = slow->next;
}
return fast;
}
};
复制复杂链表
![在这里插入图片描述](https://img-blog.csdnimg.cn/2019062816171180.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxMTAyNDczNDU0Ng==,size_16,color_FFFFFF,t_70)
Node* copyRandomList(Node* head) {
if (head == NULL)
return NULL;
Node* cur = head;
while (cur != nullptr){
Node* copy = new Node(cur->val, nullptr, nullptr);
copy->next = cur->next;
cur->next = copy;
cur = copy->next;
}
cur = head;
while (cur != nullptr){
if (cur->random != nullptr)
cur->next->random = cur->random->next;
cur = cur->next->next;
}
cur = head;
Node *copylist = head->next;
while (cur && cur->next){
Node* copy = cur->next;
cur->next = cur->next->next;
cur = copy;
}
return copylist;
}