您的位置:首页 > 健康 > 美食 > 网店毕业论文_武汉大学人民医院东院_seo关键词排名教程_电商seo优化是什么

网店毕业论文_武汉大学人民医院东院_seo关键词排名教程_电商seo优化是什么

2024/12/23 2:36:22 来源:https://blog.csdn.net/2301_81348661/article/details/143726000  浏览:    关键词:网店毕业论文_武汉大学人民医院东院_seo关键词排名教程_电商seo优化是什么
网店毕业论文_武汉大学人民医院东院_seo关键词排名教程_电商seo优化是什么

目录

① 203.移除链表元素

② 206.反转链表

③ 876.链表的中间节点 

④ 返回倒数第k个节点(面试题)

⑤ 21.合并两个有序链表

⑥ 160.相交链表 

⑦ 138.随机链表的复制(深拷贝)


f65f2847c0974f8690bdeca8d6867a0f.gif


① 203.移除链表元素

d489e57558fa48ac852b2823a86dffa2.png

 

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/typedef struct ListNode ListNode;
struct ListNode* removeElements(struct ListNode* head, int val) {ListNode*newHead = NULL, *newTail = NULL;ListNode*pcur = head;while(pcur != NULL){if(pcur->val != val){if(newHead == NULL){//将新链表的头尾指针指向原链表头节点newHead = newTail = pcur;}else{newTail->next = pcur;newTail = newTail->next;}}pcur = pcur->next;}if(newTail != NULL){newTail->next = NULL;}                                                                        return newHead;
}

 

 


 

 

② 206.反转链表

 39aa95d0fe0944d18f93268d49f5a543.png

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/struct ListNode* reverseList(struct ListNode* head) {struct ListNode* prev = NULL;//哨兵位struct ListNode* cur = head;//头节点while (cur) {// 哨兵位(prev)  节点1(cur)  节点2(cur->next)struct ListNode* next = cur->next;//创建一个中间节点//开始改变链表的方向cur->next = prev;//节点2先指向节点1的前一个节点prev = cur;//哨兵位往后移动cur = next;//节点1向后移动}return prev;
}

 

 


 

 

③ 876.链表的中间节点 

f316c4e6c2dd4673b9b27bc84ad73646.png

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/struct ListNode* middleNode(struct ListNode* head) {//慢指针(一次走一步)struct ListNode* slow = head;//快指针(一次走两步)struct ListNode* fast = head;while(fast && fast->next){slow = slow->next;fast = fast->next->next;}return slow; 
}

 

 


 

 

④ 返回倒数第k个节点(面试题)

7d8c78d2f96c4b049b90362c8144706b.png

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
int kthToLast(struct ListNode* head, int k) {struct  ListNode *fast = head, *slow = head;//本题采用的是相对法://      fast先运动k个节点//      假设该链表的节点个数是 m+k 个//      则先走k个节点,剩下fast指针到null指针时,即走了m个节点//      此时,slow指针就剩余k个节点while(k--){fast = fast->next;}while(fast != NULL){slow = slow->next;fast = fast->next;   }return slow->val;
}

 

 


 

 

⑤ 21.合并两个有序链表

 

33bda6c74a77458eae7845409d991cd1.png

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {if(list1 == NULL){return list2;}if(list2 == NULL){return list1;}struct ListNode* l1 = list1;struct ListNode* l2 = list2;struct ListNode* newHead, *newTail;                                                             newHead = newTail = NULL;while(l1  &&  l2){//比大小if(l1->val < l2->val){if(newHead == NULL){newHead = newTail = l1;}else{newTail->next = l1;newTail = newTail->next;}l1 = l1->next;}else{if(newHead == NULL){newHead = newTail = l2;}else{newTail->next = l2;newTail = newTail->next;}l2 = l2->next;}}if(l1){newTail->next = l1;}if(l2){newTail->next = l2;}return newHead;
}

 

 


 

 

⑥ 160.相交链表 

ec919a9804a74894a852973faae88385.png

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {struct ListNode* curA = headA,  *curB = headB;int lenA = 1, lenB = 1;while(curA->next){curA = curA->next;++lenA;}while(curB->next){curB = curB->next;++lenB;}if(curA != curB){return NULL;}//假设法int gap = abs(lenA - lenB);struct ListNode* longList = headA, *shortList = headB;if(lenB > lenA){longList = headB;shortList = headA;}`while(gap--){longList = longList->next;}while(longList != shortList){longList = longList->next;shortList = shortList->next;}return longList;}

 

 


 

 

⑦ 138.随机链表的复制(深拷贝)

1369640e4ebc41a29064d21f90ee31cf.png

53510b52c9804686aa1fe9e98b51005d.png

 

/*** Definition for a Node.* struct Node {*     int val;*     struct Node *next;*     struct Node *random;* };*/struct Node* copyRandomList(struct Node* head) {struct Node* cur = head;while(cur){struct Node * copy = (struct Node*)malloc(sizeof(struct Node));//赋值copy->val = cur->val;//将copy链表插入原链表中copy->next = cur->next;cur->next = copy;//cur向后移动cur = copy->next;}//将copy链表中random指针的指向与原链表中的指针对调cur = head;while(cur){struct Node* copy = cur->next;if(cur->random == NULL){copy->random = NULL;}else{copy->random = cur->random->next;}cur = copy->next;}cur = head;struct Node* copyHead = NULL,* copyTail =NULL;while(cur){struct Node* copy = cur->next;if(copyTail == NULL){copyHead = copyTail = copy;}else{copyTail->next = copy;copyTail = copyTail->next;}//cur向后移动cur = copy->next;}return copyHead;
}

 


f1676cb2fcf24d61b464dfe0a9decfdd.gif

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com