Given a linked list, swap every two adjacent nodes and return its head.

You may not modify the values in the list's nodes, only nodes itself may be changed.

Example:

Given 1->2->3->4, you should return the list as 2->1->4->3.

---------------------------------------------------------------------------
中文大意是两两交换其中相邻的结点。有非递归和递归的解法,在非递归的解法上,要画下图,辅助理解。 C++代码:
非递归:
/**
* 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) {
ListNode *dummy = new ListNode(-),*cur = dummy;
cur->next = head;
while(cur->next && cur->next->next){
ListNode *t = cur->next->next;
cur->next->next = t->next;
t->next = cur->next;
cur->next = t;
cur =cur->next->next;
}
return dummy->next;
}
};

递归:

/**
* 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 || !head -> next){
return head;
}
ListNode *t = head->next;
head->next = swapPairs(head->next->next);
t->next = head;
return t;
}
};

参考博客:http://www.cnblogs.com/grandyang/p/4441680.html

(链表 递归) leetcode 24. Swap Nodes in Pairs的更多相关文章

  1. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  2. leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法

    Swap Nodes in Pairs  Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  3. [LeetCode]24. Swap Nodes in Pairs两两交换链表中的节点

    Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3 ...

  4. leetcode 24. Swap Nodes in Pairs(链表)

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  5. LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)

    题目标签:Linked List 题目给了我们一组 linked list,让我们把每对nodes 互换位置. 新键一个dummy node,然后遍历list,每次建立 s1 和 s2 记录两个点,然 ...

  6. LeetCode 24. Swap Nodes in Pairs(交换链表中每两个相邻节点)

    题意:交换链表中每两个相邻节点,不能修改节点的val值. 分析:递归.如果以第三个结点为头结点的链表已经两两交换完毕(这一步递归实现---swapPairs(head -> next -> ...

  7. [leetcode]24. Swap Nodes in Pairs交换链表的节点

    感觉这个题后台的运行程序有问题,一开始自己想的是反转链表那道题的方法,只是隔一个节点执行一次,但是没有通过,TLE了,但是很奇怪,并没有死循环,就是最后返回的时候超时. 最后的思路就是很简单的进行交换 ...

  8. [LeetCode] 24. Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  9. Leetcode 24. Swap Nodes in Pairs(详细图解一看就会)

    题目内容 Given a linked list, swap every two adjacent nodes and return its head. You may not modify the ...

随机推荐

  1. WhiteHat Contest 11 : re1-100

    ELF文件,运行一下是要求输密码 die查了一下无壳 直接拖入ida 可以发现 这是它的判断函数 也就是说输入的总长度是42位第一个字符是123也就是0x7b 也就是'{'然后10位是"53 ...

  2. gauss——seidel迭代

    转载:https://blog.csdn.net/wangxiaojun911/article/details/6890282 Gauss–Seidelmethod 对应于形如Ax = b的方程(A为 ...

  3. endnote中文格式“,等”的修改

    https://www.howsci.com/endnote-eng-cn-refer-etal.html

  4. DeepLearning训练方法

    1.BN层训练技巧 缩小输入尺寸,这样可以提高batchsize的大小,在BN层适应了该数据集后,固定住BN层参数,放大输入尺寸继续训练 2.语义分割中解决网络输出尺寸与原尺寸的gap方法(如 1/8 ...

  5. 微服务 Micro services

    微服务 (Microservices) 是一种软件架构风格,它是以专注于单一责任与功能的小型功能区块 (Small Building Blocks) 为基础,利用模组化的方式组合出复杂的大型应用程序, ...

  6. 11/1/2018模拟 Max

    题面 也就是说, 随机序列RMQ.(\(n \le 8388608\), \(m \le 8*10^6\)) 解法 我写了笛卡尔树+tarjan 然而听神仙说, 因为数据随机, 建完树暴力找lca就行 ...

  7. FTC诉高通垄断案苹果从中受益

    据外媒报道,美国当地时间周二,美国联邦贸易委员会(FTC)诉芯片制造商高通公司(Qualcomm)垄断案进入了终结辩论阶段.这意味着,这起审判也进入最后阶段,它可能颠覆高通在智能手机时代取得成功的至关 ...

  8. kubernetes 基本命令

    查询命令: kubectl get pods -n kube-system kubectl get ClusterRole -n kube-system kubectl get ClusterRole ...

  9. java json转换(一)

    主要使用了2个类 JsonConvert.class 和 ConvertHelper.class 由于常规转json.只要model牵涉到复杂的关联实体对象.那么就会出现 深度循环的错误. 因此这里通 ...

  10. Alice's Chance POJ - 1698(按时间点建边)

    Alice's Chance Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7791   Accepted: 3174 De ...