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. PAT——1024. 科学计数法

    科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+,即数字的整数部分只有1位,小数部分至少有1位 ...

  2. Linux mysql 5.5.10 二进制安装过程记录和 修改 密码 登录

    1.useradd clouder2.解压缩mysql.tar.bz2到/home/clouder2.mv /etc/my.cnf /etc/my.cnf.bak3./home/clouder/mys ...

  3. [iOS]一些第三方库

    BHInfiniteScrollView 地址 https://github.com/qylibohao/BHInfiniteScrollView 功能 图片轮播 TZImagePickerContr ...

  4. throws、throw和try catch

    在学习代理模式的时候,编写动态生成代理类.java文件时,用try{}catch(){}捕获异常发现catch(Exception e)报错,得换成catch(Throwable e),然后又查了查两 ...

  5. C++练习 | 求解二叉树的高度

    int h(BTree *bt) { if(bt->lchild==NULL&&bt->rchild==NULL) ; if(bt->lchild!=NULL& ...

  6. A - Chess Placing CodeForces - 985A

    You are given a chessboard of size 1 × n. It is guaranteed that n is even. The chessboard is painted ...

  7. MySQL常用:Got a packet bigger than 'max_allowed_packet' bytes & MySQL开远程服务

    1. 数据导入时出现错误 Got a packet bigger than 'max_allowed_packet' bytes 通过终端进入mysql控制台 mysql>show VARIAB ...

  8. 使用babel

    1).Babel支持NPM包形式的安装,打开命令行窗口,切换到项目根目录,命令如下 npm install babel-cli 2).安装成功后,在package.json文件里添加如下代码 &quo ...

  9. PHP目前比较常见的五大运行模式

    做 php 开发的应该都知道 php 运行模式概念吧,本文将要和大家分享的是关于php目前比较常见的五大运行模式:包括cgi .fast-cgi.cli.isapi.apache模块的DLL ,下面作 ...

  10. Node.js发送电子邮件

    电子邮件作为最广泛使用的一种网络服务和我们的工作学习生活已经密不可分.接受信用卡的账单,注册网站,找回密码等等都需要用到它.今天在就来学习下如何在node下来发送邮件,为当前做的项目提供邮箱校验的功能 ...