Swap Nodes in Pairs 交換連結串列的每兩個節點
只允許使用一個申請空間,申請一個pre節點指向head節點,用指標交換,如果pre後存在兩個節點就進行一次指標交換操作
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head==nullptr||head->next==nullptr)
return head;
ListNode* pre = new ListNode(INT_MIN);
pre->next = head;
head = pre;
while(pre->next!=nullptr){
if(pre->next->next!=nullptr){
ListNode* p = pre->next->next->next;
pre->next->next->next = pre->next;
pre->next = pre->next->next;
pre->next->next->next = p;
pre = pre->next->next;
}
else{
pre = pre->next;
}
}
return head->next;
}
};
相關推薦
Swap Nodes in Pairs 交換連結串列的每兩個節點
只允許使用一個申請空間,申請一個pre節點指向head節點,用指標交換,如果pre後存在兩個節點就進行一次指標交換操作/** * Definition for singly-linked list. * struct ListNode { * int val; *
LintCode:交換連結串列當中兩個節點
連結串列 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {}
交換連結串列當中兩個節點
題目 給你一個連結串列以及兩個權值v1和v2,交換連結串列中權值為v1和v2的這兩個節點。保證連結串列中節點權值各不相同,如果沒有找到對應節點,那麼什麼也不用做。 注意事項 你需要交換兩個節點而
[leetcode]24. Swap Nodes in Pairs交換鏈表的節點
運行 鏈表 log tle first body blog 交換 ext 感覺這個題後臺的運行程序有問題,一開始自己想的是反轉鏈表那道題的方法,只是隔一個節點執行一次,但是沒有通過,TLE了,但是很奇怪,並沒有死循環,就是最後返回的時候超時。 最後的思路就是很簡單的進行交換
Leetcode刷題記——24. Swap Nodes in Pairs(交換成對結點)
一、題目敘述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should
[Swift]LeetCode24. 兩兩交換連結串列中的節點 | Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3->4, you should return the list as 2->1-&g
【LeetCode題解】24_兩兩交換連結串列中的節點(Swap-Nodes-in-Pairs)
目錄 描述 解法一:迭代 思路 Java 實現 Python 實現 複雜度分析 解法二:遞迴(不滿足空間複雜度要求) 思路 Java 實現 Python 實現 複雜度分析 更多 LeetCo
LeetCode : 24. 兩兩交換連結串列中的節點(Swap Nodes In Pairs)解答
24. 兩兩交換連結串列中的節點 給定一個連結串列,兩兩交換其中相鄰的節點,並返回交換後的連結串列。 示例: 給定 1->2->3->4, 你應該返回 2->1->4->3. 說明: 你的演算法只能使用常數的額
【LeetCode】Swap Nodes in Pairs(兩兩交換連結串列中的節點)
這是LeetCode裡的第24題。 題目要求: 給定一個連結串列,兩兩交換其中相鄰的節點,並返回交換後的連結串列。 示例: 給定1->2->3->4, 你應該返回2->1->4->3. 說明: 你的演算法只能使用常數
LeetCode24. 兩兩交換連結串列中的節點 Swap Nodes in Pairs(C語言)
題目描述: 給定一個連結串列,兩兩交換其中相鄰的節點,並返回交換後的連結串列。 示例: 給定 1->2->3->4, 你應該返回 2->1->4->3. 說明: 你的演算法只能使用常數的額外空間。 你不能
LeetCode-24:Swap Nodes in Pairs(兩兩交換連結串列中的節點) -- Medium
題目: Given a linked list, swap every two adjacent nodes and return its head. 例子: Example 1: Given 1->2->3->4, you s
swap nodes in pairs(成對的交換連結串列結點)
題目描述 Given a linked list, swap every two adjacent nodes and return its head. For example, Given1->2->3->4, you should return the list
LeetCode 24 — Swap Nodes in Pairs(兩兩交換連結串列中的節點)
Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3->4, you should return the l
[LeetCode]24. Swap Nodes in Pairs兩兩交換連結串列中的節點
Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3->4, you should return the list as
【連結串列&交換鄰節點】Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the
Swap Nodes in Pairs 連結串列交換相鄰的節點
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->
leetcode鏈表--16、swap-nodes-in-pairs(成對交換鏈表結點)
ive push 返回 pre head 交換 while const int 題目描述 Given a linked list, swap every two adjacent nodes and return its head. For example, Given
24.成對的交換節點(24.Swap Nodes in Pairs)
solution == cnblogs || des nbsp 位置 class 空間 題目: 給定一個鏈表,交換每兩個相鄰的節點並返回其頭。 例如,給定1->2->3->4,您應該返回列表2->1->4->3。 您的算法應該僅使用恒定空
(Java) LeetCode 24. Swap Nodes in Pairs —— 兩兩交換鏈表中的節點
only title The reverse elf link 反轉鏈表 ould not Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1-&
[LeetCode] Swap Nodes in Pairs 成對交換節點
Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3->4, you should return the list as 2->1-&