leetcode 24 两两交换链表中的节点

简介: leetcode 24 两两交换链表中的节点

两两交换链表中的节点


e3b4f02a011e40eca5cb694c5562c0e4.png

自己写

原理为两个交换,交换参数为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;
}

卡尔

11d2e9cbce374dacbbec80745f53b161.png

 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;
    }
};


相关文章
|
5月前
【力扣】-- 移除链表元素
【力扣】-- 移除链表元素
56 1
|
5月前
Leetcode第21题(合并两个有序链表)
这篇文章介绍了如何使用非递归和递归方法解决LeetCode第21题,即合并两个有序链表的问题。
75 0
Leetcode第21题(合并两个有序链表)
|
5月前
LeetCode第二十四题(两两交换链表中的节点)
这篇文章介绍了LeetCode第24题的解法,即如何通过使用三个指针(preNode, curNode, curNextNode)来两两交换链表中的节点,并提供了详细的代码实现。
50 0
LeetCode第二十四题(两两交换链表中的节点)
|
5月前
(剑指offer)18、删除链表的节点—22、链表中倒数第K个节点—25、合并两个排序的链表—52、两个链表的第一个公共节点(2021.12.07)
(剑指offer)18、删除链表的节点—22、链表中倒数第K个节点—25、合并两个排序的链表—52、两个链表的第一个公共节点(2021.12.07)
70 0
|
5月前
|
索引
力扣(LeetCode)数据结构练习题(3)------链表
力扣(LeetCode)数据结构练习题(3)------链表
131 0
|
6月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
7月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
79 6
|
7月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
153 2
|
4月前
|
机器学习/深度学习 人工智能 自然语言处理
280页PDF,全方位评估OpenAI o1,Leetcode刷题准确率竟这么高
【10月更文挑战第24天】近年来,OpenAI的o1模型在大型语言模型(LLMs)中脱颖而出,展现出卓越的推理能力和知识整合能力。基于Transformer架构,o1模型采用了链式思维和强化学习等先进技术,显著提升了其在编程竞赛、医学影像报告生成、数学问题解决、自然语言推理和芯片设计等领域的表现。本文将全面评估o1模型的性能及其对AI研究和应用的潜在影响。
111 1
|
6月前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口