Description

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

Example

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

Challenge

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

解题:将链表中的结点换一下位置。代码如下:

/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/ public class Solution {
/**
* @param head: a ListNode
* @return: a ListNode
*/
public ListNode swapPairs(ListNode head) {
// write your code here
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
while(pre.next != null && pre.next.next != null){
ListNode t = pre.next.next;
pre.next.next = t.next;
t.next = pre.next;
pre.next = t;
pre = t.next;
}
return dummy.next;
}
}

451. Swap Nodes in Pairs【LintCode java】的更多相关文章

  1. Leetcode 24题 两两交换链表中的节点(Swap Nodes in Pairs))Java语言求解

    题目描述: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3->4,你应该返回 ...

  2. 376. Binary Tree Path Sum【LintCode java】

    Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...

  3. 372. Delete Node in a Linked List【LintCode java】

    Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...

  4. 245. Subtree【LintCode java】

    Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...

  5. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  6. 433. Number of Islands【LintCode java】

    Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...

  7. 423. Valid Parentheses【LintCode java】

    Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...

  8. 422. Length of Last Word【LintCode java】

    Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...

  9. 420. Count and Say【LintCode java】

    Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...

随机推荐

  1. python -- 将string转换成dict的方法

    装载自:http://smilejay.com/2014/10/convert_string_to_dict_python/ 我将数据库连接相关的一些用户名/密码/host/port等各种东西作为一个 ...

  2. webapi 异常处理

    参考:https://docs.microsoft.com/zh-cn/aspnet/web-api/overview/error-handling/exception-handling ①HttpR ...

  3. 杂记(那些我还容易混淆的c和c++知识)

    1: 定义一个对象时先调用基类的构造函数.然后调用派生类的构造函数:析构的时候恰好相反:先调用派生类的析构函数.然后调用基类的析构函数.2:  多态性具体体现在运行和编译两个方面:在程序运行时的多态性 ...

  4. 【CSS3基础-Flex布局】

    关于Flex 背景 在flex布局出现以前,常用的水平和垂直居中对齐方式有很多.flex布局的出现基本规范了这一过程. 通过justify-content和align-items两个属性即解决了水平居 ...

  5. 8. DBNEWID 工具(使用nid命令修改db name及dbid)

    以下参考自:https://www.2cto.com/database/201305/207860.html Oralce官网:https://docs.oracle.com/cd/E11882_01 ...

  6. javascript 异步操作,串形执行,并行执行

    单线程模型 单线程模型指的是,JavaScript 只在一个线程上运行.也就是说,JavaScript 同时只能执行一个任务,其他任务都必须在后面排队等待. 注意,JavaScript 只在一个线程上 ...

  7. LL(1)文法--递归下降程序

    递归下降程序 递归下降程序一般是针对某一个文法的.而递归下降的预测分析是为每一个非终结符号写一个分析过程,由于文法本身是递归的,所以这些过程也是递归的. 以上是前提. Sample 假如给的是正规式子 ...

  8. CentOS7.6离线安装JDK1.8

    卸载CentOS自带的openJDK: 查看openJDK命令:rpm -qa|grep java 结果: java-1.8.0-openjdk-headless-1.8.0.181-7.b13.el ...

  9. WebGl 缩放(矩阵变换)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Redis之Redis主从复制

    概念: 主从复制就是主机数据更新后,根据配置和策略,自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主. 配置: (1)“一主二仆”策略 准备三台redis服务器 ...