[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 return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

【题意】

给定一个链表,交换每两个相邻的节点,并返回它的头。

你的算法应该使用常数空间。您不得修改在列表中的值,只有节点本身是可以改变的。

【分析】

【代码】

/*********************************
*   日期:2014-01-29
*   作者:SJF0115
*   题号: 24.Swap Nodes in Pairs
*   来源:http://oj.leetcode.com/problems/swap-nodes-in-pairs/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *swapPairs(ListNode *head) {
        if (head == NULL){
            return head;
        }

        ListNode *dummy = (ListNode*)malloc(sizeof(ListNode));
        dummy->next = head;

        ListNode *nodeA,*nodeB = NULL;

        ListNode *pre = dummy,*cur = head;
        //至少2个节点采用交换
        while(cur != NULL && cur->next != NULL){
            nodeA = cur;
            nodeB = cur->next;
            //交换
            nodeA->next = nodeB->next;
            nodeB->next = nodeA;
            pre->next = nodeB;
            //更新pre,cur
            pre = nodeA;
            cur = nodeA->next;
        }
        return dummy->next;
    }
};
int main() {
    Solution solution;
    int A[] = {1,2,3,4,5};
    ListNode *head = (ListNode*)malloc(sizeof(ListNode));
    head->next = NULL;
    ListNode *node;
    ListNode *pre = head;
    for(int i = 0;i < 4;i++){
        node = (ListNode*)malloc(sizeof(ListNode));
        node->val = A[i];
        node->next = NULL;
        pre->next = node;
        pre = node;
    }
    head = solution.swapPairs(head->next);
    while(head != NULL){
        printf("%d ",head->val);
        head = head->next;
    }
    return 0;
}

【代码2】

class Solution {
public:
    ListNode *swapPairs(ListNode *head) {
        return reverseKGroup(head,2);
    }
private:
    ListNode *reverseKGroup(ListNode *head, int k) {
        if (head == NULL || k < 2){
            return head;
        }

        ListNode *dummy = (ListNode*)malloc(sizeof(ListNode));
        dummy->next = head;

        ListNode *pre = dummy,*cur = NULL,*tail = NULL;
        //统计节点个数
        int count = 0;
        while(pre->next != NULL){
            pre = pre->next;
            count++;
        }
        //反转次数
        int rCount = count / k;
        int index = 0;
        //反转元素的前一个
        pre = dummy;
        //反转元素第一个即翻转后的尾元素
        tail = dummy->next;
        //共进行rCount次反转
        while(index < rCount){
            int i = k - 1;
            //反转
            while(i > 0){
                //删除cur元素
                cur = tail->next;
                tail->next = cur->next;
                //插入cur元素
                cur->next = pre->next;
                pre->next = cur;
                i--;
            }
            pre = tail;
            tail = pre->next;
            index++;
        }
        return dummy->next;
    }
};

利用: LeetCode之Reverse Nodes in k-Group



目录
相关文章
Leetcode 24.Swap Nodes in Pairs
 给你一个链表,交换相邻两个节点,例如给你 1->2->3->4,输出2->1->4->3。   我代码里在head之前新增了一个节点newhead,其实是为了少写一些判断head的代码。
81 0
|
索引
LeetCode 336. Palindrome Pairs
给定一组唯一的单词, 找出所有不同 的索引对(i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串。
232 0
LeetCode 336. Palindrome Pairs
【LeetCode】Palindrome Pairs(336)
  Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a   palindrome.
149 0
|
算法
LeetCode 24 Swap Nodes in Pairs(交换序列中的结点)(Linked List)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/49803287 翻译 给定一个链表,调换每两个相邻节点,并返回其头部。
1208 0
|
12月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
179 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
134 6
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
295 2
|
12月前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
193 3
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
10月前
|
机器学习/深度学习 人工智能 自然语言处理
280页PDF,全方位评估OpenAI o1,Leetcode刷题准确率竟这么高
【10月更文挑战第24天】近年来,OpenAI的o1模型在大型语言模型(LLMs)中脱颖而出,展现出卓越的推理能力和知识整合能力。基于Transformer架构,o1模型采用了链式思维和强化学习等先进技术,显著提升了其在编程竞赛、医学影像报告生成、数学问题解决、自然语言推理和芯片设计等领域的表现。本文将全面评估o1模型的性能及其对AI研究和应用的潜在影响。
242 1
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
250 7