#反轉連結串列_C++版 #反轉連結串列_Java版 @FDDLC
問題描述:輸入一個連結串列,反轉連結串列後,輸出新連結串列的表頭。
方法一(C++版):使用vector並反轉
#include <bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(!pHead) return NULL;
vector<ListNode*> v;
for(ListNode* pCurrent = pHead; pCurrent; pCurrent = pCurrent->next) {
v.push_back(pCurrent);
}
//反轉 vector<ListNode*> v
reverse(v.begin(), v.end());
//構造一個新的連結串列
int i_end = v.size()-1;
for(int i = 0; i < i_end; i++) {
v[i]->next = v[i+1];
}
v[i_end]->next = NULL; //最後一個結點的指標域為空
return v[0];
}
};
//輸出連結串列
void printList(ListNode* pHead) {
for(ListNode* pCurrent = pHead; pCurrent; pCurrent = pCurrent->next) {
cout<<pCurrent->val<<" ";
}
cout<<endl;
}
int main() {
ListNode node1 = ListNode(1);
ListNode node2 = ListNode(2);
ListNode node3 = ListNode(3);
node1.next = &node2;
node2.next = &node3;
node3.next = NULL;
//反轉前
printList(&node1);
//進行反轉
Solution solution;
ListNode* pHead = solution.ReverseList(&node1);
//反轉後
printList(pHead);
return 0;
}
方法二(C++版):原來是結點i的指標域存著結點i+1的地址,現在只要讓結點i的指標域存結點i-1的地址即可。
#include <bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
//注意:不要輕易使用pHead->next,因為pHead如果為空,那麼pHead->next就會報錯!
ListNode *pPre = NULL;
ListNode *pCurrent = pHead;
while(pCurrent) {
ListNode *pNext = pCurrent->next; //備份結點i的指點域(即結點i+1的地址),因為在下一步會被重寫
//結點i的指標域存它前1個結點(即結點i-1)的地址
pCurrent->next = pPre;
//在上一步中結點i-1已經完成了它的使命,現在可以把結點i的地址賦給它了
pPre = pCurrent;
//在前兩步中結點i已經完成了它的使命,現在可以把備份的結點i+1的地址賦給它了
//有請下一位:結點i+1
pCurrent = pNext;
}
//迴圈結束後pCurrent為NULL,pPre為最後一個結點
return pPre;
}
};
//輸出連結串列
void printList(ListNode* pHead) {
for(ListNode* pCurrent = pHead; pCurrent; pCurrent = pCurrent->next) {
cout<<pCurrent->val<<" ";
}
cout<<endl;
}
int main() {
ListNode node0 = ListNode(0);
ListNode node1 = ListNode(1);
ListNode node2 = ListNode(2);
node0.next = &node1;
node1.next = &node2;
node2.next = NULL;
//反轉前
printList(&node0);
//進行反轉
Solution solution;
ListNode* pHead = solution.ReverseList(&node0);
//反轉後
printList(pHead);
return 0;
}
附上1個錯誤版本(其實先來個 if(pHead == null) return null; 就OK了):
方法三(Java版本):
public class Solution {
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
public ListNode ReverseList(ListNode head) {
ListNode pre = null;
ListNode current = head;
ListNode next;
while(current != null) {
next = current.next;
current.next = pre;
pre = current;
current = next;
}
return pre;
}
public void printList(ListNode node) {
while(node != null) {
System.out.print(node.val + " ");
node = node.next;
}
System.out.println();
}
public static void main(String args[]) {
Solution solution = new Solution();
ListNode node0 = solution.new ListNode(0);
ListNode node1 = solution.new ListNode(1);
ListNode node2 = solution.new ListNode(2);
node0.next = node1;
node1.next = node2;
node2.next = null;
//反轉前
solution.printList(node0);
ListNode head = solution.ReverseList(node0);
//反轉後
solution.printList(head);
}
}
相關文章
- 反轉連結串列
- 連結串列面試題(二)---連結串列逆序(連結串列反轉)面試題
- java實現連結串列反轉Java
- 反轉連結串列(C++簡單區)C++
- JavaScript從反轉陣列到連結串列反轉JavaScript陣列
- 反轉連結串列、合併連結串列、樹的子結構
- 【LeetCode-連結串列】面試題-反轉連結串列LeetCode面試題
- 如何在Java中反轉單連結串列?Java
- leetcode 反轉連結串列LeetCode
- TypeScript 實現連結串列反轉TypeScript
- 1025 反轉連結串列
- 資料結構之連結串列:206. 反轉連結串列資料結構
- 【C++】“反轉連結串列”相關的題目C++
- Java單連結串列反轉圖文詳解Java
- leetcode 92 反轉連結串列ⅡLeetCode
- 面試必備的「反轉連結串列」面試
- java不用api實現單連結串列反轉(二)JavaAPI
- 劍指offer面試16 反轉連結串列面試
- C++:用棧實現反轉連結串列,超簡單!C++
- [每日一題] 第二題:反轉連結串列每日一題
- 定義一個函式,輸入一個連結串列的頭節點,反轉該連結串列並輸出反轉後連結串列的頭節點函式
- 演算法題:反轉一個單連結串列&判斷連結串列是否有環演算法
- java 反轉單連結串列,偶爾會遇到的面試題Java面試題
- leetcode 206.反轉連結串列LeetCode
- LeetCode-092-反轉連結串列 IILeetCode
- leetcode206. 反轉連結串列LeetCode
- Java版-資料結構-連結串列Java資料結構
- 資料結構和演算法——Go實現單連結串列並且反轉單連結串列資料結構演算法Go
- 【演算法題】反轉連結串列的兩種方法演算法
- leetCode206 反轉連結串列ILeetCode
- Java資料結構和演算法(六)—演算法—反轉連結串列Java資料結構演算法
- LeetCode-Python-(206)反轉連結串列LeetCodePython
- 實現反轉連結串列--遞迴、迭代、雙指標、棧遞迴指標
- LeetCode每日一題:反轉連結串列(No.206)LeetCode每日一題
- 圖解帶頭節點的單連結串列的反轉操作圖解
- LeetCode題解(Offer24):反轉連結串列(Python)LeetCodePython
- 利用遞迴方法實現連結串列反轉、前N個節點反轉以及中間部分節點反轉遞迴
- JAVA基礎:語言中連結串列和雙向連結串列的實現(轉)Java