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, ...
随机推荐
- 部署Jar包到远程Maven仓库
在使用maven开发工程时,模块A可能会依赖模块B的jar包,如果两个模块都是在一个工程里,只需要在模块A的pom文件中加入模块B的依赖信息,模块A就可以加载模块B的jar包.但如果模块A与模块B在不 ...
- Maven profile动态选择配置条件
背景 在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置.以及一些软件运行过程中的基本配置, ...
- UIWebView 获取网页标题
- (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *urlString = webView.request.URL.absolu ...
- oracle查询用户的权限
DBA_* 描述的是数据库中的所有对象 ALL_* 描述的是当前用户有访问权限的所有对象 USER_* 描述的是当前用户所拥有的所有对象 查看所有用户: select * from dba_user ...
- No MyBatis mapper was found in '[com.wuji.springboot]' package. Please check your configuration
No MyBatis mapper was found in '[com.wuji.springboot]' package. Please check your configuration. 这个原 ...
- 自己动手写一个简易对象关系映射,ORM(单例版和数据库池版)
准备知识 DBUtils模块 <<-----重点 DBUtils是Python的一个用于实现数据库连接池的模块 此连接池有两种连接模式: DBUtils提供两种外部接口: Persist ...
- 如何创建一个新的vue项目
一.cnpm安装 1.百度node官网,进入官网下载安装包安装好node环境 2.成功后打开cmd命令行工具,执行node-v命令,查看node版本号,如果能输出版本号说明安装成功 3.推荐使用淘宝 ...
- jquery里操作json相关的方法和实例
$.getJSON("/html/aijquery/JSON.js",function(d){ $.each(d,function(i,v){ $( ...
- 利用.htaccess文件将多个子域名解析至对应的子目录
对于不支持子域名解析但是支持 .htaccess 的主机来说,这个功能就非常有用了 假设有主域名 ppios.com,子域名 yspx.ppios.com 和 ask.ppios.com,设置结果为访 ...
- IOS 可以连接 蓝牙BLE设备,但是无法发现服务(原创)
注:转载请标明文章来源,感谢支持作者劳动! 一.问题描述 用iphone手机上的nRF connect软件调试蓝牙通信. 1.nRF52蓝牙demo电路板,烧录一个SDK的程序,iphone手机可以成 ...