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) {
if(head == null || head.next == null)
return head; ListNode h = new ListNode(0);
h.next = head;
ListNode p = h; while(p.next != null && p.next.next != null){
//use t1 to track first node
ListNode t1 = p;
p = p.next;
t1.next = p.next; //use t2 to track next node of the pair
ListNode t2 = p.next.next;
p.next.next = p;
p.next = t2;
} return h.next;
}
}

解题思路:这题主要涉及到链表的操作,没什么特别的技巧,注意不要出错就好。最好加一个头结点,操作起来会很方便。

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param a ListNode
# @return a ListNode
def swapPairs(self, head):
if head == None or head.next == None:
return head
dummy = ListNode(0); dummy.next = head
p = dummy
while p.next and p.next.next:
tmp = p.next.next
p.next.next = tmp.next
tmp.next = p.next
p.next = tmp
p = p.next.next
return dummy.next

22 Swap Nodes in Pairs的更多相关文章

  1. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

  2. Leetcode-24 Swap Nodes in Pairs

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

  3. 24. Swap Nodes in Pairs

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

  4. [LintCode] Swap Nodes in Pairs 成对交换节点

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

  5. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

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

  6. 【LeetCode练习题】Swap Nodes in Pairs

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

  7. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

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

  9. Leetcode 线性表 Swap Nodes in Pairs

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...

随机推荐

  1. java读取配置到Hash表里

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; im ...

  2. Java Compare接口

    在Java集合框架中有两种比较接口: Comparable 接口和 Comparator 接口. 一.Comparable 接口  public interface Comparable<T&g ...

  3. Java学习笔记day08_day09_对象实例化_private_this

    1.类与对象 类就是一个模版. 对象的实例化就是根据模版类, 使用new关键字创建实际的对象. 2.类的定义及创建对象 类的定义格式: public class 类名{ //属性(变量) 数据类型 变 ...

  4. 自定义element-ui主题

    自定义element主题颜色:主要参考这个文章https://blog.csdn.net/wangcuiling_123/article/details/78513245,再自己做了一遍成功.感谢. ...

  5. android Activity启动过程(二)从ActivityManagerService的startActivity到栈顶Activity的onPause过程

    ActivityManagerService.startActivity() ActvityiManagerService.startActivityAsUser() ActivityStackSup ...

  6. (转)centOS wget的使用

    摘要:CentOS wget是一个从网络上自动下载文件的自由工具.它支持HTTP,HTTPS和FTP协议,可以使用HTTP代理. 所谓的自动下载是指,CentOS wget可以在用户退出系统的之后在后 ...

  7. 牛客网Java刷题知识点之父类中的私有内容,子类是否具备? 子类不可直接,但可间接访问父类中的私有内容?

    不多说,直接上干货!  父类中的私有内容,子类是否具备? 答:不具备 子类不可直接,但可间接访问父类中的私有内容 这样情况,开发中不所见,但是,面试的时候,必考非常常见.

  8. @enable跟@import注解

    参考文章: 讲@import的相关内容:https://blog.csdn.net/u012437781/article/details/78626134 讲为什么registrar没有注入:http ...

  9. 工作采坑札记:2. Hadoop中MultipleInputs的使用陷阱

    1. 背景 近日在一个Hadoop项目中使用MultipleInputs增加多输入文件时,发现相同路径仅会加载一次,导致后续的统计任务严重失真.本博文旨在记录异常的排查及解决方案. 2. 情景重现 ( ...

  10. Linux大文件快速处理小方法

    背景 工作中使用MapReduce任务导出一批含有路径的文件,共计行数300W+,需要检测文件是否在对应的服务器中存在,而文件所在的服务器并非hadoop集群的服务器,因此打算采用bash脚本进行.具 ...