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. JDK代码中的优化 之 “avoid getfield opcode”

    在查看String类源码时,常看到注释 /* avoid getfield opcode */ 如 trim()方法 public String trim() { int len = value.le ...

  2. CC2540 OSAL 学习其中原理,以及 给任务 添加 一个事件(定时发送串口消息)

    参考学习大神博客: http://blog.csdn.net/feilusia/article/details/51083953 : http://blog.csdn.net/xiaoleiacmer ...

  3. ASP.NET Core Middleware (转载)

    What is Middleware? Put simply, you use middleware components to compose the functionality of your A ...

  4. 关于修改计算机名称导致无法启动Oracle监听?

    解决方法: 修改D:\app\‘admin’\product\11.2.0\dbhome_1\NETWORK\ADMIN\路径下的listener.ora和tnsnames.ora文件配置中的host ...

  5. Node调试

    之前调试node代码简单粗暴,直接在代码打印日志,控制台观察日志,效率低下~ 原来后端node代码也可以通过Chrome断点调试,以下是调试方法. (1)找到node启动的进程 ps aux|grep ...

  6. Delphi获取Android下GPS的NMEA 0183数据

    下面的程序,可以实现Android下获取GNSS的NMEA0183数据: unit utAndroidNmea; interface uses Androidapi.JNIBridge, Androi ...

  7. zookeeper无法启动"Unable to load database on disk

    QuorumPeerMain,ResourceManager都没有起来 resourcemanager.log如下 2018-09-28 23:17:02,787 FATAL org.apache.h ...

  8. Security 安全框架1

    security 过滤器链 检查请求是否有请求信息-主要校验规则 UsernamePasswordAuthenticationFilter: 处理表单登录, 请求是否带用户名密码 BasicAuthe ...

  9. 从零开始的Python学习Episode 14——日志操作

    日志操作 一.logging模块 %(message)s 日志信息 %(levelno)s 日志级别 datefmt 设置时间格式 filename 设置日志保存的路径 level 设置日志记录的级别 ...

  10. Go黑帽子

    使用go语言来实现python黑帽子和绝技的代码 1.unix密码破解器 package main import( "bufio" "flag" "i ...