LeetCode 24 Swap Nodes in Pairs(交换序列中的结点)

翻译

给定一个链表,调换每两个相邻节点,并返回其头部。

例如,
给定 1->2->3->4, 你应该返回的链表是 2->1->4->3。

你的算法必须使用唯一不变的空间。

你也不能修改列表中的值,只有节点本身是可以改变的。

原文

Give 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.

分析

我们就以题目中给出的1,2,3,4作为示例,将其作为两部分

1,2 -- 3,4

或者我画个图出来更加直观吧……

代码

/**
 * 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 == NULL) return NULL;
        if(head->next == NULL) return head;

        ListNode* temp = head->next;
        head->next = swapPairs(temp->next);
        temp->next = head;

        return temp;
    }
};
时间: 2024-09-12 18:46:30

LeetCode 24 Swap Nodes in Pairs(交换序列中的结点)的相关文章

[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 lis

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, onl

[LeetCode] Maximum Swap 最大置换

Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get. Example 1: Input: 2736 Output: 7236 Explanation: Swap the number 2 and the number 7. Example 2: Inp

指针-C++中利用子函数交换main()中的一个int数组的值,交换地址为什么不可?

问题描述 C++中利用子函数交换main()中的一个int数组的值,交换地址为什么不可? 某书思考题 只改动子函数 实现主函数中数组排序我写了3种子函数 注释的都是可以正常用的 最上面的不可以(排序没变)不知道是为啥(指针不是代表地址吗 既然形参无法传回那我改变地址应该也可以啊) #include<iostream>#include<iomanip>#include<cstdlib> //pauseusing namespace std;//排序不变void swap(

求一个序列中,第k个数

//求一个序列中,第k个数 1.排序,输出a[k-1]冒泡for(i=1;i<=n-1;i++)//控制循环次数 for(j=0;j<=n-i-1;j++)//不是n-i+1 if(a[i]>a[i+1]]) 交换,每排一次最大的移到最后 定位选择排序for(i=0;i<n-1;i++)//控制循环次数 for(j=i+1;j<n;j++)// if(a[i]>a[j]) 交换 2.先取出前k个排序,再取未排序的,若大于a[k-1],则忽略,否则插入适当位置并移去a[k

(算法导论习题解problem2.4)寻找一个序列中逆序对的数量

一个序列的逆序对是这样的两个元素, 对于序列A而言, i>j且A[i]<A[j], 于是A[i]和A[j]就形成一个逆序对. 研究一个序列中逆序对的数量是有实际意义的, 对于插入排序而言, 它排序的时间与待排序序列的逆序对数量成正比. 下面给出求出一个序列中逆序对数量的算法,类似于归并排序中使用的分治算法:一个序列的逆序对数量为它的左半部分逆序对的数量,加上右半部分逆序对的数量, 最后在合并的时候左半部分元素大于右半部分元素的数量.这几乎和归并算法的过程一模一样,只是在归并的时候加入了计数的操

c语言-从键盘输入每小时登录网络的用户数到一个有24个整型元素的数组中。编程以如下格式显示一个报告

问题描述 从键盘输入每小时登录网络的用户数到一个有24个整型元素的数组中.编程以如下格式显示一个报告 从键盘输入每小时登录网络的用户数到一个有24个整型元素的数组中.编程以如下格式显示一个报告: ? 时间? 登录人数? 所占比例 ?0:00 – 1:00 ?1 0.3 ?1:00 – 2:00 ?2 0.7 ?- ?10:00 - 11:00 ?27 9.0 ?11:00 - 12:00 ?28 9.3 - ?23:00 - 0:00 ?8 2.7 ?最大登录人数28发生在11:00到12:00

c语言-C语言 给定一个整数序列和一个数k,求这个序列中第k小的数。

问题描述 C语言 给定一个整数序列和一个数k,求这个序列中第k小的数. C语言 给定一个整数序列和一个数k,求这个序列中第k小的数. 我的程序 #include<stdio.h> int n[10000]; void Nok() { int i=0,j=0,t,k,q=0; char c; scanf("%d",&n[i++]); c=getchar(); while(c!='n') { scanf("%d",&n[i++]); c=ge

《Python Cookbook(第3版)中文版》——1.10 从序列中移除重复项且保持元素间顺序不变

1.10 从序列中移除重复项且保持元素间顺序不变 1.10.1 问题 我们想去除序列中出现的重复元素,但仍然保持剩下的元素顺序不变. 1.10.2 解决方案 如果序列中的值是可哈希(hashable)的,那么这个问题可以通过使用集合和生成器轻松解决.示例如下[1]: def dedupe(items): seen = set() for item in items: if item not in seen: yield item seen.add(item) 这里是如何使用这个函数的例子: >>