初階數據結構:鏈表相關題目練習(補充)

目錄

  • 1. 單鏈表相關練習題
    • 1.1 移除鏈表元素
    • 1.2 反轉鏈表
    • 1.3 鏈表的中間結點
    • 1.4 鏈表的倒數第k個結點
    • 1.5 合并兩個有序鏈表
    • 1.6 鏈表分割
    • 1.7 鏈表的回文結構
    • 1.8 相交鏈表
    • 1.9 判斷一個鏈表中是否有環
    • 1.10 尋找環狀鏈表相遇點
    • 1.11 鏈表的深度拷貝

1. 單鏈表相關練習題

注:單鏈表結構上存在一定缺陷,所以鏈表相關的題目一般都針對與單鏈表。

1.1 移除鏈表元素

題目要求:在這里插入圖片描述
題目信息:

  1. 頭節點head
  2. 移除值val

題目鏈接:
移除鏈表元素

方法(順序處理法):

思路:分析鏈表結構與結點所處的位置(是否為空鏈表,結點是否為頭結點),分情況依次處理。

過程演示:
在這里插入圖片描述

struct ListNode* removeElements4(struct ListNode* head, int val)
{struct ListNode* pre = NULL;struct ListNode* cur = head;while (cur){if (cur->val == val){//頭刪if (cur == head){head = head->next;free(cur);cur = head;}else//中間刪{pre->next = cur->next;free(cur);cur = pre->next;}}else{pre = cur;cur = cur->next;}}return head;
}

1.2 反轉鏈表

題目要求:
在這里插入圖片描述
題目鏈接:
反轉鏈表

過程演示:

struct ListNode* reverseList(struct ListNode* head) 
{struct ListNode* pre = NULL;struct ListNode* mid = head;struct ListNode* cur = NULL;if(head){cur = head->next;}while(mid){mid->next = pre;pre = mid;mid = cur;if(cur){cur = cur->next;}}return pre;}

1.3 鏈表的中間結點

題目要求:
在這里插入圖片描述
題目鏈接:
鏈表的中間結點

過程演示(快慢指針法):

struct ListNode* middleNode(struct ListNode* head) 
{struct ListNode* fast = head;struct ListNode* slow = head;while(fast != NULL && fast->next != NULL){fast = fast->next->next;slow = slow->next;}return slow;
}

1.4 鏈表的倒數第k個結點

題目要求:
在這里插入圖片描述
題目鏈接:
倒數第k個結點

過程演示:

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) 
{struct ListNode* cur = pListHead;struct ListNode* pre = pListHead;while(cur && k--){cur = cur->next;}if(k > 0){pre = NULL;}while(cur){pre = pre->next;cur = cur->next;}return pre;
}

1.5 合并兩個有序鏈表

題目要求:
在這里插入圖片描述
題目鏈接:
合并兩個鏈表

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) 
{struct ListNode* newhead = NULL;struct ListNode* cur = NULL;struct ListNode* newnode = NULL;if(list1 == NULL)return list2;if(list2 == NULL)return list1;while(list1 != NULL && list2 != NULL){if(list1->val <= list2->val){newnode = list1;list1 = list1->next;}else{newnode = list2;list2 = list2->next;}if(newhead == NULL){newhead = newnode;newnode->next = NULL;cur = newhead;}else{//在遍歷過程中,list1 或 list2 會等于 NULLcur->next = newnode;if(newnode != NULL){newnode->next = NULL;cur = cur->next;}}}//有一個鏈表本就為空if(list1){cur->next = list1;}if(list2){cur->next = list2;}return newhead;
}

1.6 鏈表分割

題目要求:
在這里插入圖片描述
題目鏈接:
合并兩個鏈表

ListNode* BuyNewNode(int val){ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));newnode->val = val;newnode->next = nullptr;return newnode;}ListNode* partition(ListNode* pHead, int x) {ListNode* newhead1 = nullptr;ListNode* end1 = nullptr;ListNode* newhead2 = nullptr;ListNode* end2 = nullptr;ListNode* cur = pHead;while(cur){if(cur->val < x){if(newhead1 == nullptr){newhead1 = BuyNewNode(cur->val);end1 = newhead1;             }else {end1->next = BuyNewNode(cur->val);end1 = end1->next;}}else {if(newhead2 == nullptr){newhead2 = BuyNewNode(cur->val);end2 = newhead2;             }else {end2->next = BuyNewNode(cur->val);end2 = end2->next;}}cur = cur->next;}if(newhead1 == nullptr){newhead1 = newhead2;}else {end1->next = newhead2;}return newhead1;}

1.7 鏈表的回文結構

題目要求:
在這里插入圖片描述
題目鏈接:
回文串

ListNode* reverse(ListNode* head){ListNode* pre = nullptr;ListNode* mid = head;ListNode* cur = head->next;while (mid){mid->next = pre;pre = mid;mid = cur;if (cur){cur = cur->next;}}return pre;}bool chkPalindrome(ListNode* A){//找相同,逆置//逐點比較//回文結構存在奇數個結點//獲取中間結點ListNode* fast = A;ListNode* slow = A;while(fast && fast->next){fast = fast->next->next;if(fast){slow = slow->next;}}//表1ListNode* newhead2 = slow->next;//表2slow->next = nullptr;ListNode* newhead1 = reverse(A);if(fast){newhead1 = newhead1->next;}while (newhead1 && newhead2 && newhead1->val == newhead2->val){newhead1 = newhead1->next;newhead2 = newhead2->next;}if (newhead1 == nullptr && newhead2 == nullptr){return true;}return false;}

1.8 相交鏈表

題目要求:
在這里插入圖片描述題目鏈接:
相交鏈表

void swap(struct ListNode** node1, struct ListNode** node2)
{struct ListNode* tmp = *node1;*node1 = *node2;*node2 = tmp;
}struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{struct ListNode* short_list1 = headA;struct ListNode* short_list2 = headA;struct ListNode* long_list1 = headB;struct ListNode* long_list2 = headB;while(short_list1 && long_list1){short_list1 = short_list1->next;long_list1 = long_list1->next;}if(short_list1){swap(&short_list1, &long_list1);swap(&short_list2, &long_list2);}while(long_list1){long_list1 = long_list1->next;long_list2 = long_list2->next;}//while((short_list2 && long_list2) || short_list2 != long_list2)while(short_list2 && long_list2 && short_list2 != long_list2){long_list2 = long_list2->next;short_list2 = short_list2->next;}return short_list2;
}

1.9 判斷一個鏈表中是否有環

題目要求:
在這里插入圖片描述
題目鏈接:
判斷是否有環

//邏輯步驟存疑
bool hasCycle(struct ListNode *head) 
{struct ListNode* fast = head;struct ListNode* slow = head;//err: while(fast && slow != fast)while(fast && fast->next){fast = fast->next->next;slow = slow->next;if(slow == fast){return true;}}return false;
}

1.10 尋找環狀鏈表相遇點

題目要求:
在這里插入圖片描述
題目鏈接:
尋找環狀鏈表相遇點

思路依靠結論:一個結點從起始點,一個結點從相遇點(快慢指針相遇點),以同速行走(一次走一步),當他們再一次初次相遇時,此相遇結點即為入環點。

struct ListNode *detectCycle(struct ListNode *head) 
{//快慢指針確定首次相遇點//起始點與相遇點出發,同速移動,最后的相遇的點即為環的進入點struct ListNode* fast = head;struct ListNode* slow = head;//遍歷尋找相遇點while(fast && fast->next){fast = fast->next->next;slow = slow->next;if(fast == slow){break;}}//判斷是否為環狀鏈表if(fast == NULL || fast->next == NULL){return NULL;}slow = head;while(fast != slow){fast = fast->next;slow = slow->next;}return fast;
}

1.11 鏈表的深度拷貝

題目要求:
在這里插入圖片描述>題目鏈接:
鏈表的深度拷貝

//思路:
//生成拷貝結點
//調整拷貝結點的指針
//還原原鏈表,鏈接拷貝結點
struct Node* BuyNewNode(int val)
{struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));newnode->val = val;newnode->next = NULL;newnode->random = NULL;return newnode;
}struct Node* copyRandomList(struct Node* head)
{struct Node* node1 = head;//判斷是否為空鏈表if(head == NULL){return head;}//創建新節點while (node1){struct Node* newnode = BuyNewNode(node1->val);newnode->next = node1->next;node1->next = newnode;node1 = node1->next->next;}//調整新節點的隨機指針struct Node* node2 = head->next;node1 = head;while (node2){if (node1->random){node2->random = node1->random->next;}node1 = node1->next->next;if (node2->next){node2 = node2->next->next;}else{node2 = node2->next;}}//還原鏈表,鏈接新鏈表node1 = head;node2 = head->next;struct Node* newhead = head->next;while (node1){node1->next = node1->next->next;if (node2->next){node2->next = node2->next->next;}node1 = node1->next;node2 = node2->next;}return newhead;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/711151.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/711151.shtml
英文地址,請注明出處:http://en.pswp.cn/news/711151.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

IEEE Transactions on Industrial Electronics工業電子TIE修改稿注意事項及提交須知

一、背景 兔年末投了一篇TIE&#xff0c;手稿初次提交的注意事項也整理成了博客IEEE Transactions on Industrial Electronics工業電子TIE論文投稿須知&#xff0c;獲得了許多點贊和收藏。最近也收到了審稿結果&#xff0c;給的意見是大修major revision&#xff0c;總之只要不…

基于springboot+vue的線上輔導班系統

博主主頁&#xff1a;貓頭鷹源碼 博主簡介&#xff1a;Java領域優質創作者、CSDN博客專家、阿里云專家博主、公司架構師、全網粉絲5萬、專注Java技術領域和畢業設計項目實戰&#xff0c;歡迎高校老師\講師\同行交流合作 ?主要內容&#xff1a;畢業設計(Javaweb項目|小程序|Pyt…

吸貓毛空氣凈化器哪個好?推薦除貓毛好的寵物空氣凈化器品牌

如今&#xff0c;越來越多的家庭選擇養寵物&#xff01;雖然家里變得更加溫馨&#xff0c;但養寵可能會帶來異味和空氣中的毛發增多可能會引發健康問題&#xff0c;這也是一個大問題。 但我不想家里到處都是異味&#xff0c;尤其是便便的味道&#xff0c;所以很需要一款能夠處…

QML中表格中數據獲取

1.在生成的動態表格中獲取某格數據的內容 import QtQuick 2.15 import QtQuick.Window 2.15import QtQuick.Controls 2.0 import Qt.labs.qmlmodels 1.0 import QtQuick.Layouts 1.15Window {width: 640height: 480visible: truetitle: qsTr("Hello World")TableMod…

數據分析-Pandas數據如何圖示規律

數據分析-Pandas數據如何圖示規律 數據分析和處理中&#xff0c;難免會遇到各種數據&#xff0c;那么數據呈現怎樣的規律呢&#xff1f;不管金融數據&#xff0c;風控數據&#xff0c;營銷數據等等&#xff0c;莫不如此。如何通過圖示展示數據的規律&#xff1f; 數據表&…

VS2015報錯:error MSB8020和MSB8036的解決方案

VS2015編譯報錯&#xff1a;error MSB8020 提示信息&#xff1a;error MSB8020: The build tools for v141 (Platform Toolset ‘v141’) cannot be found. To build using the v141 build tools, please install v141 build tools. Alternatively, you may upgrade to the c…

小程序框架接口-getApp

框架接口-getApp getApp() 用于獲取小程序全局唯一的 App 實例&#xff0c;通過小程序應用實例可實現數據或方法的共享 &#x1f4cc; 注意事項&#xff1a; 1.不要在 App() 方法中使用 getApp() &#xff0c;使用 this 就可以拿到 app 實例通過 getApp() 獲取實例之后&#x…

Android13 Audio框架

一、Android 13音頻代碼結構 1、framework: android/frameworks/base 1.AudioManager.java &#xff1a;音頻管理器&#xff0c;音量調節、音量UI、設置和獲取參數等控制流的對外API 2.AudioService.java &#xff1a;音頻系統服務&#xff08;java層&#xff09;&#xff0c…

多模態論文閱讀-LLaVA

Visual Instruction Tuning Abstract1. Introduction2. Related Work3. GPT-assisted Visual Instruction Data Generation4. Visual Instruction Tuning4.1 Architecture4.2 Training 5 Experiments5.1 Multimodal Chatchot5.2 ScienceQA 6 Conclusion Abstract 使用機器生成…

JS中判斷是否存在逗號,如果存在給去掉

.includes() 方法判斷是否存在 split("需要去掉的字符串").join(" ") 去重的方法 去重復 劃分后拼接

網絡——DHCP服務器、DNS服務器實驗

網絡——DHCP服務器、DNS服務器實驗 一、DHCP服務器實驗 DHCP——動態主機配置協議,用來管理ip地址的分配。網絡中的每臺計算機都有至少一個ip地址。在Windows網絡連接對話框中可以設置成自動獲取ip地址,這樣主機作為DHCP client就可以自動從DHCP server獲取ip地址了。 DHC…

live555學習 - 環境準備

環境&#xff1a;Ubuntu 16.04.7 ffmpeg-6.1 1 代碼下載 最新版本&#xff1a; http://www.live555.com/liveMedia/public/ 歷史版本下載 https://download.videolan.org/pub/contrib/live555/ 選擇版本live.2023.01.19.tar.gz ps&#xff1a;沒有選擇新版本是新版本在…

數據庫優化建議

盡量控制單表數據量的大小&#xff0c;建議控制在 500 萬以內 500 萬并不是 MySQL 數據庫的限制&#xff0c;過大會造成修改表結構&#xff0c;備份&#xff0c;恢復都會有很大的問題。可以用歷史數據歸檔&#xff08;應用于日志數據&#xff09;&#xff0c;分庫分表&#xf…

阿里開源的Java診斷利器Arthas

一.什么是Arthas 1.為什么需要Arthas 通常&#xff0c;本地開發環境無法訪問生產環境。如果在生產環境中遇到問題&#xff0c;則無法使用 IDE 遠程調試。更糟糕的是&#xff0c;在生產環境中調試是不可接受的&#xff0c;因為它會暫停所有線程&#xff0c;導致服務暫停。 開…

探索Apple Vision Pro:創新技術帶來的多彩應用世界

Apple Vision Pro是一款具有前沿技術的設備,可以與現實世界進行交互,讓用戶在虛擬世界中享受各種應用。以下是一些值得注意的Vision Pro應用: AR演示環境:Vision Pro上的AR應用主要是基于AR的演示環境,這些應用可以讓用戶在現實世界中體驗虛擬世界。游戲:Vision Pro上有一…

c語言統計字符

本題要求編寫程序&#xff0c;輸入10個字符&#xff0c;統計其中英文字母、空格或回車、數字字符和其他字符的個數。 輸入格式: 輸入為10個字符。最后一個回車表示輸入結束&#xff0c;不算在內。 輸出格式: 在一行內按照 letter 英文字母個數, blank 空格或回車個數, d…

鴻蒙Harmony應用開發—ArkTS聲明式開發(鼠標事件)

在鼠標的單個動作觸發多個事件時&#xff0c;事件的順序是固定的&#xff0c;鼠標事件默認透傳。 說明&#xff1a; 從API Version 8開始支持。后續版本如有新增內容&#xff0c;則采用上角標單獨標記該內容的起始版本。目前僅支持通過外接鼠標觸發。 onHover onHover(event: …

vue中element-ui中的el-button自定義icon圖標

實現&#xff1a; button的icon屬性自定義一個圖標名稱&#xff0c;這個自定義的圖標名稱會默認添加到button下i標簽的class上&#xff0c;我們只需要設置i標簽的樣式就可以了。 1. 控制臺顯示的代碼 2 .圖片展示 3. 按鈕上使用自定義的icon 完整代碼&#xff1a; <el-but…

postman切換成黑色主題

postman安裝以后默認是白色背景&#xff0c;如果想要切換成黑色的&#xff0c;大家可以按照下圖箭頭指示來操作。 1打開設置 2在Themes頁面選擇黑色主題

物聯網常見協議篇

在物聯網環境中&#xff0c;物聯網協議承擔著關鍵作用&#xff0c;而新手了解物聯網協議如傳輸協議、通訊協議和行業協議等。 一、物聯網協議 物聯網協議是物聯網環境中的關鍵組成部分&#xff0c;它承擔著設備間通信和數據傳輸的重要任務。這些協議根據其作用的不同&#xff…