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, ...
随机推荐
- 安装Oracle 18.4 RAC 遇到ORA-40238报错
场景: Aix 7.2 上安装Oracle 18.4 RAC 执行root.sh脚本,ASM failed to start !查看日志文件:ORA-40238: invalid linear alg ...
- Objective-C 方法交换实践(三) - Aspects 源码解析
一.类与变量 AspectOptions typedef NS_OPTIONS(NSUInteger, AspectOptions) { AspectPositionAfter = 0, /// 原方 ...
- PHP设置Redis key在当天有效|SCP对拷如何连接指定端口(非22端口)的远程主机
$redis->set($key,$value); $expireTime = mktime(23, 59, 59, date("m"), date("d" ...
- js时间与毫秒互相转换
1)日期转换为毫秒 如果格式是:yyyy/mm/dd hh:mm:ss可以直接转换.var oldTime = (new Date("2018/07/09 14:13:11")). ...
- Delphi Firemonkey在主线程 异步调用函数(延迟调用)
先看下面的FMX.Layouts.pas中一段代码 procedure TCustomScrollBox.MouseDown(Button: TMouseButton; Shift: TShiftSt ...
- T+API实现
目前用友的T+,官方提供了APi给用户直接调用,但是必3须申请,而且还必须是企业用户,估计是收费的. 这边接到一个开发外包,调用其他应用的数据同步到t+里,我首先想到的就是直接调用t+提供的APi,这 ...
- 需求:加一个下拉框选择条件改变饼图内外环 饼图:百度echarts提供
1.1:下拉框条件:后台取得ViewBag传给前台 MonitorController: public ActionResult BigData(): //下拉框筛选条件 var result = M ...
- 浏览器窗口输入网址后发生的一段事情(http完整请求)
1.DNS查询得到IP 输入的是域名,需要进行dns解析成IP,大致流程: 如果浏览器有缓存,直接使用浏览器缓存,否则使用本机缓存,再没有的话就是用host 如果本地没有,就向dns域名服务器查询(当 ...
- md5.digest()与md5.hexdigest()之间的区别及转换
举给例子 md5 = hashlib.md5('adsf') md5.digest() //返回: '\x05\xc1*(s48l\x94\x13\x1a\xb8\xaa\x00\xd0\x8a' # ...
- android studio 调试技巧(简直太好用)
android studio 调试技巧(简直太好用) 说到android studio的调试,很多人可能会说,这有什么可讲的不就是一个断点调试么,刚开始我也是这么认为的,直到我了解之后,才发现,调试原 ...