数据结构与算法——链表题目实现
文章目錄
- 1.鏈表逆序
- 1.1 題目描述
- 1.2鏈表逆序的C++實現
- 2.反轉鏈表
- 2.1 題目描述
- 2.2 反轉鏈表的C++實現
- 3.求兩個鏈表的交點
- 3.1 題目描述
- 3.2 C++實現——set
- 3.3 C語言實現——鏈表長度實現
- 4.鏈表求環
- 4.1 題目描述
- 4.2 C++實現
- 4.3 C語言實現——快慢指針
- 5.分隔鏈表
- 5.1 題目描述
- 5.2 C++實現
- 6.復制帶隨機指針的鏈表
- 6.1 題目描述
- 6.2 C++實現
- 7.排序鏈表的合并
- 7.1 題目描述
- 7.2 C++實現
- 8.多個排序鏈表的合并
- 8.1 題目描述
- 8.2 C++實現
- 8.3 C++實現——分治
1.鏈表逆序
1.1 題目描述
反轉一個單鏈表。
示例:
\qquad輸入: 1->2->3->4->5->NULL
\qquad輸出: 5->4->3->2->1->NULL
1.2鏈表逆序的C++實現
#include<iostream> using namespace std;struct ListNode {int val;ListNode* next;ListNode(int x):val(x),next(NULL){} }; class Solution { public:ListNode* reverseList(ListNode* head) {ListNode* new_head = NULL;while (head) {ListNode* next;next = head->next;head->next = new_head;new_head = head;head = next;}return new_head;} };2.反轉鏈表
2.1 題目描述
給你單鏈表的頭指針 head 和兩個整數 left 和 right ,其中 left <= right 。請你反轉從位置 left 到位置 right 的鏈表節點,返回 反轉后的鏈表 。
2.2 反轉鏈表的C++實現
struct ListNode {int val;ListNode* next;ListNode(int x):val(x),next(NULL){} }; class Solution { public:ListNode* reverseBetween(ListNode* head, int left, int right) {int change_len = right-left+1;ListNode *pre_head=NULL;ListNode *result=head;while(head&&--left){pre_head=head;head=head->next;}ListNode *modify_list_tail=head;ListNode *new_head=NULL;while(head&&change_len){ListNode *next=head->next;head->next=new_head;new_head=head;head=next;change_len--;}modify_list_tail->next=head;if(pre_head){pre_head->next=new_head;}else{result=new_head;}return result;} };3.求兩個鏈表的交點
3.1 題目描述
編寫一個程序,找到兩個單鏈表相交的起始節點。
3.2 C++實現——set
struct ListNode {int val;ListNode* next;ListNode(int x):val(x),next(NULL){} }; class Solution { public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {set<ListNode*> Node_set;while(headA){Node_set.insert(headA);headA=headA->next;}while(headB){if(Node_set.find(headB)!=Node_set.end()){return headB;}headB=headB->next;}return NULL;} };3.3 C語言實現——鏈表長度實現
步驟1:計算headA和headB的長度
步驟2:將較長鏈表的指針移動到和較短鏈表指針對齊的地方
步驟3:兩個鏈表同時移動,當兩者指向同一個結點時,結點被找到
4.鏈表求環
4.1 題目描述
給定一個鏈表,返回鏈表開始入環的第一個節點。 如果鏈表無環,則返回 null。
為了表示給定鏈表中的環,我們使用整數 pos 來表示鏈表尾連接到鏈表中的位置(索引從 0 開始)。 如果 pos 是 -1,則在該鏈表中沒有環。注意,pos 僅僅是用于標識環的情況,并不會作為參數傳遞到函數中。
4.2 C++實現
class Solution { public:ListNode *detectCycle(ListNode *head) {set<ListNode *> node_list;while(head){if(node_list.find(head)!=node_list.end()){return head;}node_list.insert(head);head=head->next;}return NULL;} };4.3 C語言實現——快慢指針
struct ListNode *detectCycle(struct ListNode *head) {struct ListNode *fast=head;struct ListNode *slow=head;while(fast!=NULL){slow=slow->next;if(fast->next==NULL){return NULL;}fast=fast->next->next;if(fast==slow){struct ListNode *ptr=head;while(ptr){if(ptr==slow){return ptr;}ptr=ptr->next;slow=slow->next;} }}return NULL; }5.分隔鏈表
5.1 題目描述
給你一個鏈表的頭節點 head 和一個特定值 x ,請你對鏈表進行分隔,使得所有 小于 x 的節點都出現在 大于或等于 x 的節點之前。
你應當 保留 兩個分區中每個節點的初始相對位置。
5.2 C++實現
class Solution { public:ListNode* partition(ListNode* head, int x) {ListNode less_head(0);ListNode more_head(0);ListNode *less_ptr=&less_head;ListNode *more_ptr=&more_head;while(head){if(head->val<x){less_ptr->next=head;less_ptr=head;}else{more_ptr->next=head;more_ptr=head;}head=head->next;}less_ptr->next=more_head.next;more_ptr->next=NULL;return less_head.next;} };6.復制帶隨機指針的鏈表
6.1 題目描述
題目描述
6.2 C++實現
#include<iostream> using namespace std;struct ListNode {int val;ListNode* next;ListNode(int x):val(x),next(NULL){} }; class Solution { public:Node* copyRandomList(Node* head) {map<Node *,int> node_map;vector<Node *> node_vec;Node *ptr=head;int i=0;while(ptr){node_map[ptr]=i;node_vec.push_back(new Node(ptr->val));ptr=ptr->next;i++;}node_vec.push_back(0);ptr=head;i=0;while(ptr){node_vec[i]->next=node_vec[i+1];if(ptr->random){int id=node_map[ptr->random];node_vec[i]->random=node_vec[id];}ptr=ptr->next;i++;}return node_vec[0];} };7.排序鏈表的合并
7.1 題目描述
將兩個升序鏈表合并為一個新的 升序 鏈表并返回。新鏈表是通過拼接給定的兩個鏈表的所有節點組成的。
7.2 C++實現
class Solution { public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {ListNode temp_head(0);ListNode *pre=&temp_head;while(l1&&l2){if(l1->val<l2->val){pre->next=l1;l1=l1->next;}else{pre->next=l2;l2=l2->next;}pre=pre->next;}if(l1){pre->next=l1;}if(l2){pre->next=l2;}return temp_head.next;} };8.多個排序鏈表的合并
8.1 題目描述
給你一個鏈表數組,每個鏈表都已經按升序排列。
請你將所有鏈表合并到一個升序鏈表中,返回合并后的鏈表。
8.2 C++實現
bool cmp(ListNode *a,ListNode *b){return a->val<b->val;} class Solution { public:ListNode* mergeKLists(vector<ListNode*>& lists) {vector<ListNode *> node_vec;int i;for(i=0;i<lists.size();i++){ListNode *head=lists[i];while(head){node_vec.push_back(head);head=head->next;}}if(node_vec.size()==0){return NULL;}sort(node_vec.begin(),node_vec.end(),cmp);for(i=1;i<node_vec.size();i++){node_vec[i-1]->next=node_vec[i];}node_vec[node_vec.size()-1]->next=NULL;return node_vec[0];} };8.3 C++實現——分治
class Solution { public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {ListNode temp_head(0);ListNode *pre=&temp_head;while(l1&&l2){if(l1->val<l2->val){pre->next=l1;l1=l1->next;}else{pre->next=l2;l2=l2->next;}pre=pre->next;}if(l1){pre->next=l1;}if(l2){pre->next=l2;}return temp_head.next;}ListNode* mergeKLists(vector<ListNode*>& lists) {if(lists.size()==0){return NULL;}if(lists.size()==1){return lists[0];}if(lists.size()==2){return mergeTwoLists(lists[0],lists[1]);}int mid=lists.size()/2;vector<ListNode *> sub1_lists;vector<ListNode *> sub2_lists;int i;for(i=0;i<mid;i++){sub1_lists.push_back(lists[i]);}for(i=mid;i<lists.size();i++){sub2_lists.push_back(lists[i]);}ListNode *l1=mergeKLists(sub1_lists);ListNode *l2=mergeKLists(sub2_lists);return mergeTwoLists(l1,l2);} };總結
以上是生活随笔為你收集整理的数据结构与算法——链表题目实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客15555 1 + 2 = 3?
- 下一篇: 计算机网络(二十一)-数据链路层设备