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.

解法:

  循环的方式:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head; head = dummy;
while (head.next != null && head.next.next != null) {
ListNode left = head.next;
ListNode right = left.next; // head -> left -> right -> ....
// to: head -> right -> left -> ....
left.next = right.next;
right.next = left;
head.next = right;
head = left;
} return dummy.next;
}
}

  递归的方式:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
} ListNode temp = head.next;
head.next = swapPairs(temp.next);
temp.next = head;
return temp;
}
}

[LeetCode] 24. Swap Nodes in Pairs ☆的更多相关文章

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

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

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

  3. [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 ...

  4. Java [leetcode 24]Swap Nodes in Pairs

    题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...

  5. Leetcode 24——Swap Nodes in Pairs

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

  6. LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java

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

  7. (链表 递归) 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 ...

  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 (交换相邻节点)

    题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description   Problem: 交换相邻的两个节点     如上 ...

随机推荐

  1. Lucky Conversion(找规律)

    Description Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive int ...

  2. Alpha 冲刺5

    队名:日不落战队 安琪(队长) 今天完成的任务 组织第五次站立式会议(半冲刺总结交流会). 完成草稿箱前端界面. 明天的计划 回收站前端界面. 尝试去调用数据. 还剩下的任务 信息修改前端界面. 遇到 ...

  3. ASLR/DEP绕过技术概览

    在经典的栈溢出模型中,通过覆盖函数的返回地址来达到控制程序执行流程(EIP寄存器),通常将返回地址覆盖为0x7FFA4512,这个地址是一条JMP ESP指令,在函数返回时就会跳转到这个地址去执行,也 ...

  4. PAT 1085 PAT单位排行 (Microsoft_zzt)

    https://pintia.cn/problem-sets/994805260223102976/problems/994805260353126400 每次 PAT 考试结束后,考试中心都会发布一 ...

  5. jenkin重新注册用户

    http://www.cnblogs.com/xiao-fy/

  6. 用putty玩linux的时候由于以前用window 习惯写完东西按一下ctrl+s 保存

    问题描述:用putty玩linux的时候由于以前用window 习惯写完东西按一下ctrl+s 保存,但是在putty一按下就不能再输入了.后来查找到:ctrl+s 是putty的一个命令大概是这样子 ...

  7. 判断一个变量是不是json,以及如何将变量转换成json

    https://blog.csdn.net/A123638/article/details/52486975这里看到一个很好的方法 // 判断变量是不是jsonisJson(variable: any ...

  8. Robotium之“去哪儿旅行”

    Robotium基于APK自动化测试,只有APK文件,没有源代码. Eclipse 默认的debug keystore可以在Windows->Preferences->Android-&g ...

  9. bootstrap练习制作网页

    导航条 <nav class="navbar navbar-default"> <div class="container-fluid"> ...

  10. 【服务器_Tomcat】Tomcat的Server Options选项

    一.配置 默认前两个是没有勾选的,应该勾选上: 在Cotext节点中有一个reloadable='true'属性,将它改为false,可以在修改java文件后不用重启服务器即可生效,但是不包括新建ja ...