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


相关文章
|
8天前
|
算法 Java
[Java·算法·中等] LeetCode21. 合并两个有序链表
[Java·算法·中等] LeetCode21. 合并两个有序链表
14 2
|
11天前
|
Java Python
二刷力扣--链表
二刷力扣--链表
|
11天前
24. 两两交换链表中的节点
24. 两两交换链表中的节点
|
12天前
|
算法
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
|
14天前
|
存储
删除链表的节点
删除链表的节点
8 0
|
11天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-2
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题
|
11天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-1
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题
|
12天前
|
索引
【LeetCode刷题】二分查找:山脉数组的峰顶索引、寻找峰值
【LeetCode刷题】二分查找:山脉数组的峰顶索引、寻找峰值
|
12天前
|
算法
【LeetCode刷题】滑动窗口解决问题:串联所有单词的子串(困难)、最小覆盖子串(困难)
【LeetCode刷题】滑动窗口解决问题:串联所有单词的子串(困难)、最小覆盖子串(困难)
|
12天前
|
算法 容器
【LeetCode刷题】滑动窗口解决问题:水果成篮、找到字符串中所有字母异位词
【LeetCode刷题】滑动窗口解决问题:水果成篮、找到字符串中所有字母异位词