leetcode 24 两两交换链表中的节点
2023-09-27 14:29:24 时间
两两交换链表中的节点
自己写
原理为两个交换,交换参数为pre和cur。
交换成功后,连接pre的前一个pre2和cur的后一个aft。
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* pre, * cur, *temp ,*aft,*pre2,*head_test;
//链表长度为0或者为1
if (head == nullptr || head->next == nullptr)return head;
ListNode test(0,nullptr);
head_test = head->next;
pre = head;
pre2 = &test;
cur = pre->next;
//链表长度大于2以上
while (cur)
{
aft = cur->next;
if (pre != nullptr|| cur != nullptr)
{
temp = cur->next;
cur->next = pre;
pre->next = temp;
pre2->next = cur;
}
else break;
pre2 = pre;
pre = aft;
if (pre != nullptr)cur = pre->next;
else break;
}
return head_test;
}
};
int main()
{
vector<int> head = { 1,2};
ListNode* head_test = new ListNode(0);
ListNode* test , *cur = head_test;
Solution a;
for (int i = 0; i < head.size(); i++)
{
ListNode* temp = new ListNode(head[i]);
cur->next = temp;
cur = cur->next;
}
cur->next = nullptr;
cur = head_test;
cout << "cur list" << endl;
while (cur->next != nullptr)
{
cout << cur->val << ' ';
cur = cur->next;
}
cout << cur->val << endl;
test = a.swapPairs(head_test->next);
while (test->next != nullptr)
{
cout << test->val << ' ';
test = test->next;
}
cout << test->val << ' ';
return 0;
}
卡尔
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* dummyHead = new ListNode(0); // 设置一个虚拟头结点
dummyHead->next = head; // 将虚拟头结点指向head,这样方面后面做删除操作
ListNode* cur = dummyHead;
while (cur->next != nullptr && cur->next->next != nullptr) {
ListNode* tmp = cur->next; // 记录临时节点
ListNode* tmp1 = cur->next->next->next; // 记录临时节点
cur->next = cur->next->next; // 步骤一
cur->next->next = tmp; // 步骤二
cur->next->next->next = tmp1; // 步骤三
cur = cur->next->next; // cur移动两位,准备下一轮交换
}
return dummyHead->next;
}
};
二刷
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head==nullptr || head->next==nullptr) return head;
ListNode *result = head->next;
ListNode *pre = new ListNode(0,head) ;
ListNode *cur1 = head;
ListNode *cur2 ;
ListNode *cur_next ;
while(cur1 != nullptr && cur1->next != nullptr)
{
cur2 = cur1->next;
cur_next = cur2->next;
pre->next = cur2;
cur2->next = cur1;
cur1->next = cur_next;
pre = cur1;
cur1 = cur_next;
}
return result;
}
};
高刷题
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head == nullptr || head->next == nullptr) return head;
ListNode* headReal = new ListNode(0);
headReal->next = head;
ListNode* tmpPre = headReal;
ListNode* tmp1 = tmpPre->next;
ListNode* tmp2 = tmpPre->next->next;
ListNode* tmpNext = tmpPre->next->next->next;
while(tmpPre != nullptr && tmp1 != nullptr && tmp2 != nullptr )
{
tmpPre->next = tmp2;
tmp2->next = tmp1;
tmp1->next = tmpNext;
tmpPre = tmpPre->next->next;
tmp1 = tmpPre->next;
if( tmp1 == nullptr) break;
tmp2 = tmpPre->next->next;
if( tmp2 == nullptr) break;
tmpNext = tmpPre->next->next->next;
}
return headReal->next;
}
};
相关文章
- LeetCode每日一练 —— 876. 链表的中间结点
- 【LeetCode】单词搜索 II [H](前缀树)
- 图解剑指 Offer II 024. 反转链表(LeetCode)
- [leetcode]Rotate List
- LeetCode_哈希表_中等_817.链表组件
- LeetCode_双指针_中等_328.奇偶链表
- LeetCode_双指针_递归_简单_206.反转链表
- LeetCode_二叉树_简单_144.二叉树的前序遍历
- LeetCode·每日一题·1019. 链表中的下一个更大节点·单调栈
- LeetCode·每日一题·1668.最大重复子字符串·模拟
- LeetCode·123.买卖股票的最佳时机 ||| ·动态规划
- LeetCode·738.单调递增的数字·贪心
- 「LeetCode」206. 反转链表
- LeetCode-21. 合并两个有序链表(java)
- LeetCode-24. 两两交换链表中的节点(Golang实现)
- [LeetCode] 678. Valid Parenthesis String 验证括号字符串
- [LeetCode] 142. Linked List Cycle II 链表中的环 II
- [LeetCode] 149. Max Points on a Line 共线点个数
- [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表
- [LeetCode] 92. Reverse Linked List II 反向链表II
- leetcode 141 环形链表
- leetcode 347 前k个高频元素
- leetcode 150 逆波兰表达式
- leetcode 160 相交链表(面试题 02.07 链表相交)
- leetcode 19 删除链表的倒数第n个节点
- leetcode 25 k个一组的翻转链表