您的位置:首页 > 房产 > 建筑 > 数据结构初阶(2)——链表OJ

数据结构初阶(2)——链表OJ

2025/4/18 5:05:23 来源:https://blog.csdn.net/2301_80065652/article/details/141397280  浏览:    关键词:数据结构初阶(2)——链表OJ

目录

1.面试题 02.02. 返回倒数第 k 个节点

2.OR36 链表的回文结构

3.160. 相交链表


1.面试题 02.02. 返回倒数第 k 个节点

思路:快慢指针,快指针先走k格,慢指针同步

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/int kthToLast(struct ListNode* head, int k){struct ListNode* fast,*slow;fast=slow=head;while(k--){fast=fast->next;}while(fast){fast=fast->next;slow=slow->next;}return slow->val;
}

2.OR36 链表的回文结构

思路:先找中间节点,然后将节点后逆置,最后一一比较

/*
struct ListNode {int val;struct ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};*/
class PalindromeList {
public:struct ListNode* middleNode(struct ListNode* head){struct ListNode* slow=head,*fast=head;while(fast&&fast->next){slow=slow->next;fast=fast->next->next;}return slow;}struct ListNode* reverseList(struct ListNode* head){struct ListNode* cur=head;struct ListNode* newhead=NULL;while(cur){struct ListNode*next=cur->next;cur->next=newhead;newhead=cur;cur=next;}return newhead;}bool chkPalindrome(ListNode* A) {struct ListNode* mid=middleNode(A);struct ListNode* rmid=reverseList(mid);while(rmid&&A){if(rmid->val!=A->val)return false;rmid=rmid->next;A=A->next;}return true;}
};

3.160. 相交链表

思路:先判断是否相交,再计算长度差值,再一一比较

/*** 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* long1=headA,*short1=headB;if(lenb>lena){long1=headB;short1=headA;}while(gap--){long1=long1->next;}while(long1!=short1){long1=long1->next;short1=short1->next;}return long1;
}

4.142. 环形链表 II

思路一:

证明:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
struct ListNode *detectCycle(struct ListNode *head) {struct ListNode* slow,*fast;slow=fast=head;while(fast&&fast->next){slow=slow->next;fast=fast->next->next;if(slow==fast){struct ListNode* meet=slow;while(meet!=head){meet=meet->next;head=head->next;}return meet;}}return NULL;
}

思路二:改为相交链表

struct ListNode* detectCycle(struct ListNode* head)
{struct ListNode* slow, * fast;slow = fast = head;while (fast && fast->next){slow = slow->next;fast = fast->next;if (slow == fast){struct ListNode* meet = slow;struct ListNode* newhead = meet->next;meet->next = NULL;return getIntersectionNode(head, newhead);//相交链表}}return NULL:
}

5.138. 随机链表的复制

思路:

/*** 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->next=cur->next;cur->next=copy;cur=copy->next;}//randomcur=head;while(cur){struct Node* copy=cur->next;if(cur->random==NULL){copy->random=NULL;}else{copy->random=cur->random->next;}cur=copy->next;}//摘取struct Node* copyhead=NULL,*copytail=NULL;cur=head;while(cur){struct Node* copy=cur->next;struct Node* next=copy->next;if(copytail==NULL){copyhead=copytail=copy;}else{copytail->next=copy;copytail=copytail->next;}cur=next;}return copyhead;
}

版权声明:

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

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