451. Swap Nodes in Pairs【LintCode java】
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】的更多相关文章
- Leetcode 24题 两两交换链表中的节点(Swap Nodes in Pairs))Java语言求解
题目描述: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3->4,你应该返回 ...
- 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 ...
- 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 ...
- 245. Subtree【LintCode java】
Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
- 422. Length of Last Word【LintCode java】
Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
随机推荐
- 优化Eclipse基本配置
eclipse有很多默认配置会造成其本身运行缓慢,特别是加载大型工程的时候,以下列举的几种方法可以优化eclipse的运行速度,加快工程的加载和构建. 关闭XML Validation 1. 关闭当前 ...
- PAT——1040. 有几个PAT
字符串APPAPT中包含了两个单词“PAT”,其中第一个PAT是第2位(P),第4位(A),第6位(T):第二个PAT是第3位(P),第4位(A),第6位(T). 现给定字符串,问一共可以形成多少个P ...
- 开源Webshell利用工具——Altman
开源Webshell利用工具--Altman keepwn @ 工具 2014-06-04 共 6114 人围观,发现 43 个不明物体收藏该文 Altman,the webshell tool,自己 ...
- JDBC——连接数据库的代码
第一步:在SCR下创建一个file,写好数据库的相关信息. #oracle数据库 driver=oracle.jdbc.driver.OracleDriver jdbcUrl=jdbc:oracle: ...
- C++练习 | 在递增序列中查找最后一个小于等于指定数的元素
#include <iostream> using namespace std; int mid,l0; int solve(int a1[],int l,int r,int x) { & ...
- Django学习笔记6(iframe、外键插入)
1.{%include 'index.html'%i} 平时很好用的iframe在django里面的不是很好用 django里面提供了{%include 'index.html'%i}的方式来取代了i ...
- encodeURIComponent编码反斜杠 \ (正则匹配)
记录一个小bug... 前言废话: 1. 功能需求:修改输入框的内容,获取字符串传给后端保存. 2. bug历程:刚开始直接获取value值传过去.后来测试发现%&这些特殊字符无法传递后,在前 ...
- 第一章 程序设计和C语言(笔记)
一.程序和程序语言 程序:完成某项事务所预设的活动方式和活动过程. 程序设计:人们描述计算机要做的工作. 对于工作过程的细节动作描述就是一个“程序”. 在一个程序描述中,总有一批预先假定的“基本动作” ...
- cannot find module 'xxx' 解决办法
先将node_module文件夹删掉 工程目录下输入命令:npm clean cache 查看package.json里有没有依赖项,有的话npm install 没有就npm install exp ...
- 杂项(乌班图、flex的使用实例)
查看乌班图当前系统版本:lsb_release -a 转载于博客园:flex的使用实例