C語言——鏈表指定區間反轉

目錄

1.創建一個鏈表

1.鏈表節點定義

2.創建新節點

3.鏈表生成(輸入)

4.鏈表輸出

2.鏈表指定區間反轉函數

1.創建啞節點

2.找到第m-1位的節點,開始?反轉

3.連接反轉后的鏈表與未反轉的鏈表

3.未使用啞節點的運行結果

這段代碼可以直接運行檢測結果

1.創建一個鏈表

1.鏈表節點定義

#include <stdio.h>
#include <stdlib.h>// 鏈表節點定義
struct ListNode {int val;struct ListNode* next;
};

2.創建新節點

// 創建新節點
struct ListNode* createNode(int value) {struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));if (newNode == NULL) {printf("內存分配失敗!\n");exit(1);}newNode->val = value;newNode->next = NULL;return newNode;
}

3.鏈表生成(輸入)

// 從數組創建鏈表
struct ListNode* createListFromArray(int arr[], int size) {if (size == 0) return NULL;struct ListNode* head = createNode(arr[0]);struct ListNode* current = head;for (int i = 1; i < size; i++) {current->next = createNode(arr[i]);current = current->next;}return head;
}// 交互式輸入創建鏈表
struct ListNode* createListInteractive() {int n, value;printf("請輸入鏈表節點個數: ");scanf("%d", &n);if (n <= 0) return NULL;printf("請輸入%d個節點的值:\n", n);scanf("%d", &value);struct ListNode* head = createNode(value);struct ListNode* current = head;for (int i = 1; i < n; i++) {scanf("%d", &value);current->next = createNode(value);current = current->next;}return head;
}

4.鏈表輸出

// 打印鏈表
void printList(struct ListNode* head) {struct ListNode* current = head;printf("鏈表: ");while (current != NULL) {printf("%d", current->val);if (current->next != NULL) {printf(" → ");}current = current->next;}printf(" → NULL\n");
}// 帶詳細信息的打印
void printListDetailed(struct ListNode* head) {struct ListNode* current = head;int count = 1;printf("鏈表詳細信息:\n");printf("地址       值   下一個節點\n");printf("-------------------------\n");while (current != NULL) {printf("%p  %2d  ", (void*)current, current->val);if (current->next != NULL) {printf("%p", (void*)current->next);} else {printf("NULL");}printf("\n");current = current->next;count++;}printf("-------------------------\n");
}

2.鏈表指定區間反轉函數

struct ListNode* reverseBetween(struct ListNode* head, int m, int n) {if (head == NULL || m == n) return head;// 創建啞節點 - 關鍵步驟!struct ListNode dummy;dummy.next = head;struct ListNode* pre = &dummy;// 移動到第 m-1 個節點for (int i = 1; i < m; i++) {pre = pre->next;}// 反轉 m 到 n 的節點struct ListNode* cur = pre->next;struct ListNode* next = NULL;struct ListNode* prev = NULL;for (int i = m; i <= n; i++) {next = cur->next;cur->next = prev;prev = cur;cur = next;}// 重新連接鏈表pre->next->next = cur;  // 新尾節點連接后繼pre->next = prev;       // 前驅連接新頭節點return dummy.next;  // 返回真正的頭節點
}

解釋一下實現過程:演示鏈表為1->2->3->4->5->NULL,m=2,n=4

1.創建啞節點

問題:處理 m=1 的特殊情況

當要從頭節點開始反轉(m=1)時:

  • 反轉后頭節點會改變(原第n個節點成為新頭)

  • 如果沒有啞節點,需要特殊處理這種情況

2.找到第m-1位的節點,開始?反轉

for (int i = 1; i < m; i++) {pre = pre->next;
}
dummy → 1 → 2 → 3 → 4 → 5 → NULL↑pre (指向節點1)

3.連接反轉后的鏈表與未反轉的鏈表

未進行連接時(僅反轉)

dummy → 1 → 2 ← 3 ← 4    5 → NULL↑          ↑    ↑pre        prev cur
反轉部分:4 → 3 → 2 → NULL

進行連接后

dummy → 1 → 4 → 3 → 2 → 5 → NULL↑    ↑    ↑    ↑    ↑pre  新頭  中間  新尾 cur

3.未使用啞節點的運行結果

我們將返回的啞節點改成head,就將會返回未使用啞節點的反轉鏈表的頭結點。

struct ListNode* reverseBetween(struct ListNode* head, int m, int n) {if (head == NULL || m == n) return head;// 創建啞節點 - 關鍵步驟!struct ListNode dummy;dummy.next = head;struct ListNode* pre = &dummy;// 移動到第 m-1 個節點for (int i = 1; i < m; i++) {pre = pre->next;}// 反轉 m 到 n 的節點struct ListNode* cur = pre->next;struct ListNode* next = NULL;struct ListNode* prev = NULL;for (int i = m; i <= n; i++) {next = cur->next;cur->next = prev;prev = cur;cur = next;}// 重新連接鏈表pre->next->next = cur;  // 新尾節點連接后繼pre->next = prev;       // 前驅連接新頭節點return head;  // 返回
}

使用啞節點

這段代碼可以直接運行檢測結果

#include <stdio.h>
#include <stdlib.h>// 鏈表節點定義
struct ListNode {int val;struct ListNode* next;
};
// 創建新節點
struct ListNode* createNode(int value) {struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));if (newNode == NULL) {printf("內存分配失敗!\n");exit(1);}newNode->val = value;newNode->next = NULL;return newNode;
}
// 從數組創建鏈表
struct ListNode* createListFromArray(int arr[], int size) {if (size == 0) return NULL;struct ListNode* head = createNode(arr[0]);struct ListNode* current = head;for (int i = 1; i < size; i++) {current->next = createNode(arr[i]);current = current->next;}return head;
}// 交互式輸入創建鏈表
struct ListNode* createListInteractive() {int n, value;printf("請輸入鏈表節點個數: ");scanf("%d", &n);if (n <= 0) return NULL;printf("請輸入%d個節點的值:\n", n);scanf("%d", &value);struct ListNode* head = createNode(value);struct ListNode* current = head;for (int i = 1; i < n; i++) {scanf("%d", &value);current->next = createNode(value);current = current->next;}return head;
}
// 打印鏈表
void printList(struct ListNode* head) {struct ListNode* current = head;printf("鏈表: ");while (current != NULL) {printf("%d", current->val);if (current->next != NULL) {printf(" → ");}current = current->next;}printf(" → NULL\n");
}// 帶詳細信息的打印
void printListDetailed(struct ListNode* head) {struct ListNode* current = head;int count = 1;printf("鏈表詳細信息:\n");printf("地址       值   下一個節點\n");printf("-------------------------\n");while (current != NULL) {printf("%p  %2d  ", (void*)current, current->val);if (current->next != NULL) {printf("%p", (void*)current->next);} else {printf("NULL");}printf("\n");current = current->next;count++;}printf("-------------------------\n");
}
struct ListNode* reverseBetween(struct ListNode* head, int m, int n) {if (head == NULL || m == n) return head;// 創建啞節點 - 關鍵步驟!struct ListNode dummy;dummy.next = head;struct ListNode* pre = &dummy;// 移動到第 m-1 個節點for (int i = 1; i < m; i++) {pre = pre->next;}// 反轉 m 到 n 的節點struct ListNode* cur = pre->next;struct ListNode* next = NULL;struct ListNode* prev = NULL;for (int i = m; i <= n; i++) {next = cur->next;cur->next = prev;prev = cur;cur = next;}// 重新連接鏈表pre->next->next = cur;  // 新尾節點連接后繼pre->next = prev;       // 前驅連接新頭節點return dummy.next;  // 返回真正的頭節點
}
// 釋放鏈表內存
void freeList(struct ListNode* head) {struct ListNode* current = head;while (current != NULL) {struct ListNode* temp = current;current = current->next;free(temp);}
}int main() {// 方法1: 從數組創建鏈表int arr[] = {1, 2, 3, 4, 5};int size = sizeof(arr) / sizeof(arr[0]);struct ListNode* head = createListFromArray(arr, size);printf("從數組創建的鏈表:\n");printList(head);printListDetailed(head);// 方法2: 交互式輸入創建鏈表/*struct ListNode* head2 = createListInteractive();printf("交互式輸入的鏈表:\n");printList(head2);freeList(head2);*/// 測試區間反轉int m = 1, n = 4;printf("\n反轉區間 [%d, %d] 后的鏈表:\n", m, n);// 這里可以調用你的reverseBetween函數head = reverseBetween(head, m, n);printList(head);// 釋放內存freeList(head);return 0;
}

運行結果

~/gpt-vue_191657 gcc linkedlist.c -o linkedlist && ./linkedlist
從數組創建的鏈表:
鏈表: 1 → 2 → 3 → 4 → 5 → NULL
鏈表詳細信息:
地址       值   下一個節點
-------------------------
0x557bae1cc2a0   1  0x557bae1cc2c0
0x557bae1cc2c0   2  0x557bae1cc2e0
0x557bae1cc2e0   3  0x557bae1cc300
0x557bae1cc300   4  0x557bae1cc320
0x557bae1cc320   5  NULL
-------------------------反轉區間 [1, 4] 后的鏈表:
鏈表: 4 → 3 → 2 → 1 → 5 → NULL

反轉函數返回值換為head后,運行結果:

~/gpt-vue_191657 gcc linkedlist_modified.c -o linkedlist_modified && ./linkedlist_modified
從數組創建的鏈表:
鏈表: 1 → 2 → 3 → 4 → 5 → NULL
鏈表詳細信息:
地址       值   下一個節點
-------------------------
0x560fb96ca2a0   1  0x560fb96ca2c0
0x560fb96ca2c0   2  0x560fb96ca2e0
0x560fb96ca2e0   3  0x560fb96ca300
0x560fb96ca300   4  0x560fb96ca320
0x560fb96ca320   5  NULL
-------------------------反轉區間 [1, 4] 后的鏈表:
鏈表: 1 → 5 → NULL

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

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

相關文章

設計一個完整可用的 Spring Boot Starter

目錄 1. 創建項目結構 2. 添加核心依賴 (pom.xml) 3. 實現核心組件 (1) 配置屬性類 (2) 服務實現類 (3) 自動配置類 4. 注冊自動配置 5. 配置元數據支持 6. 打包發布 7. 其他項目引用 (1) 添加依賴 (2) 配置參數 (3) 使用服務 設計要點 要設計一個完整可用的 Spr…

Bright Data 代理 + MCP :解決 Google 搜索反爬的完整方案

個人主頁&#xff1a;chian-ocean 專欄 引言 人工智能技術和大數據的發展&#xff0c;實時訪問網頁數據成為許多應用的核心需求。相比傳統方案依賴靜態或定期更新的數據&#xff0c;AI可以實時抓取和分析網頁上的及時更新的信息&#xff0c;迅速適應變化的環境&#xff0c;提…

Java基礎第4天總結(多態)

package com.itheima.duotai;public class Animal {String name "動物";public void run(){System.out.println("動物會跑~~~");} }package com.itheima.duotai;public class Wolf extends Animal{String nama "狼";Overridepublic void run(…

Git克隆時遇到“Filename too long“錯誤的完美解決方案

Git克隆時遇到"Filename too long"錯誤的完美解決方案 問題描述 在使用Git克隆項目時&#xff0c;你是否遇到過這樣的錯誤&#xff1a; $ git clone gitexample.com:project.git Cloning into project... remote: Enumerating objects: 1883, done. remote: Count…

分享一個基于Python與spark大數據的護膚品市場用戶行為分析與可視化平臺,基于hadoop的護膚品使用行為追蹤與分析可視化平臺的設計與實現

&#x1f495;&#x1f495;作者&#xff1a;計算機源碼社 &#x1f495;&#x1f495;個人簡介&#xff1a;本人八年開發經驗&#xff0c;擅長Java、Python、PHP、.NET、Node.js、Spark、hadoop、Android、微信小程序、爬蟲、大數據、機器學習等&#xff0c;大家有這一塊的問題…

頁面中嵌入Coze的Chat SDK

Coze 為將 AI 聊天機器人(Bot)嵌入您的網頁提供了兩種主流方式:Web SDK 和 API 接口調用。它們分別適用于不同的場景,下面我將為您介紹這兩種方法,并提供一些選擇建議。 特性 Web SDK API 接口調用 實現方式 引入一段JS代碼,快速嵌入一個預制的聊天窗口 通過HTTP API發送…

DataEase+MaxKB:讓BI再多個“A”

一、前言當前DataEase BI更多聚焦于BI展示層&#xff0c;然而&#xff0c;在與組件Copilot 以及后續計劃替換的 Sqlbot的融合方面&#xff0c;目前仍存在一些亟待解決的問題&#xff0c;當它們嘗試與 DataEase 進行結合應用時&#xff0c;出現了兩種較為突出的狀況。一方面&…

VUE 的彈出框實現圖片預覽和視頻預覽

這是一個基于Vue3封裝的媒體預覽組件&#xff0c;主要功能包括&#xff1a;多格式支持&#xff1a;可同時預覽圖片和視頻圖片操作功能&#xff1a;縮放&#xff08;支持滾輪縮放和按鈕控制&#xff09;旋轉&#xff08;90度增量旋轉&#xff09;拖拽&#xff08;僅在放大狀態下…

【Linux基礎知識系列】第一百零九篇 - 使用shell的輸入與輸出重定向

在 Linux 系統中&#xff0c;Shell 是用戶與操作系統交互的界面&#xff0c;通過命令行輸入命令來執行各種操作。輸入與輸出重定向是 Shell 編程中非常重要的概念&#xff0c;它允許用戶將命令的輸出保存到文件中&#xff0c;或者從文件中讀取輸入&#xff0c;從而實現更靈活的…

Redis面試精講 Day 30:Redis面試真題解析與答題技巧

【Redis面試精講 Day 30】Redis面試真題解析與答題技巧 在“Redis面試精講”系列的第30天&#xff0c;我們迎來收官之作——Redis面試真題解析與答題技巧。這一天的核心目標是&#xff1a;幫助你系統化梳理前29天所學知識&#xff0c;掌握高頻面試題的解題思路&#xff0c;提升…

設計模式:單例模式(Singleton Pattern)

文章目錄一、單例模式的概念二、單例模式的結構三、常見實現方式3.1 餓漢式單例3.2 懶漢式單例一、單例模式的概念 單例模式&#xff08;Singleton Pattern&#xff09;是一種創建型設計模式&#xff0c;它的核心思想是&#xff1a;保證在一個進程中&#xff0c;某個類僅有一個…

Swift 解法詳解 LeetCode 362:敲擊計數器,讓數據統計更高效

文章目錄 摘要 描述 題解答案 題解代碼分析 代碼講解 示例測試及結果 時間復雜度 空間復雜度 總結 摘要 “敲擊計數器”這道題聽上去像個小游戲里的功能,但其實它背后對應的是一個常見的需求:在過去一段時間內統計事件發生的次數。比如網站的訪問量統計、API 調用次數限制、…

coze工作流200+源碼,涵蓋AI文案生成、圖像處理、視頻生成、自動化腳本等多個領域

AI 博主風哥在github分享了 200 實用生產力coze工作流&#xff0c;涵蓋AI文案生成、圖像處理、視頻生成、自動化腳本等多個領域&#xff0c;導入即用&#xff0c;項目地址https://github.com/Hammer1/cozeworkflows github下載慢也可前往該地址下載https://pan.baidu.com/s/1fC…

AI與SEO關鍵詞協同優化

內容概要 人工智能&#xff08;AI&#xff09;技術的迅猛發展正深刻變革著搜索引擎優化&#xff08;SEO&#xff09;的實踐方式&#xff0c;特別是在關鍵詞策略這一核心領域。兩者的深度融合&#xff0c;為企業在數字海洋中精準導航提供了前所未有的強大工具。通過AI驅動的智能…

【Unity開發】Unity核心學習(二)

二、動畫基礎 1、Animation動畫窗口 &#xff08;1&#xff09;介紹&#xff08;2&#xff09;Animation窗口功能2、創建編輯動畫 面板變化&#xff1a;動畫文件界面&#xff1a;3、Animator動畫狀態機 &#xff08;1&#xff09;有限狀態機概念&#xff08;2&#xff09;Anima…

NETSDK1045 當前 .NET SDK 不支持將 .NET 8.0 設置為目標。請將 .NET 5.0 或更低版本設置為目標,或使用支持

C# 項目中的目標框架無法修改并且顯示為空 嚴重性 代碼 說明 項目 文件 行 禁止顯示狀態 錯誤 NETSDK1045 當前 .NET SDK 不支持將 .NET 8.0 設置為目標。請將 .NET 5.0 或更低版本設置為目標&#xff0c;或使用支持 .NET 8.0 的 .NET SDK 版本。 Padim C:\Program …

MNIST 數據集mnist.npz詳解

MNIST 數據集是機器學習領域最著名的數據集之一&#xff0c;全稱為"Modified National Institute of Standards and Technology"數據庫。它包含了大量手寫數字的圖像&#xff0c;是入門機器學習和深度學習的經典數據集。1. MNIST 數據集概述 60,000 張訓練圖像 10,00…

深入理解HTTPS:從概念到實戰優化

深入理解HTTPS&#xff1a;從概念到實戰優化一&#xff1a;概述二&#xff1a;工作流程三&#xff1a;創建自簽名證書四&#xff1a;案例1&#xff09;案例一&#xff1a;HTTPS 搭建2&#xff09;案例二&#xff1a;HTTP/2 搭建3&#xff09;案例三&#xff1a;HTTP 重定向 HTT…

MySQL數據備份與恢復全攻略

一、數據備份與恢復按照備份方式分類&#xff1a;物理備份&#xff0c;直接復制數據庫的物理文件&#xff0c;可以直接拷貝和恢復&#xff1b;邏輯備份&#xff0c;通過SQL語句導出數據庫結構和數據&#xff0c;可用于不同版本和不同類型的MySQL數據庫之間的數據遷移。按照數據…

單機多卡間大張量傳輸迷惑行為?

老鐵們我最近真的好慘&#x1f62d;&#xff0c;一個大模型在單機多卡上運行就是出錯&#xff0c;debug看的老眼昏花&#xff0c;最后發現大張量在設備間直接傳輸會有很發癲的行為&#xff0c;還請大家幫我看看&#x1f647;?摒棄屎山一樣的代碼&#xff0c;簡單運行下列腳本i…