leetcode — swap-nodes-in-pairs
/**
* Source : https://oj.leetcode.com/problems/swap-nodes-in-pairs/
*
* Created by lverpeng on 2017/7/12.
*
* Given a linked list, swap every two adjacent nodes and return its head.
*
* For example,
* Given 1->2->3->4, you should return the list as 2->1->4->3.
*
* Your algorithm should use only constant space. You may not modify the values in the list,
* only nodes itself can be changed.
*
*/
public class SwapNodeInPairs {
/**
* 交换相邻两个node 的位置
*
* 记录下当前节点的下一个节点, node = current.next
* 将当前节点的下一个指向,下下个,current.next = node.next
* 将下一个节点指向当前节点,node.next = current
* 将上一个节点的下一个指向当前节点,las.next = current
*
* @param head
* @return
*/
public Node swap (Node head) {
if (head == null) {
return null;
}
Node pointer = head;
Node next = head.next;
Node last = null;
while (next != null) {
pointer.next = next.next;
next.next = pointer;
// 记录head
if (pointer == head) {
head = next;
}
if (last != null) {
last.next = next;
}
last = pointer;
// 更新pointer,next
pointer = pointer.next;
if (pointer != null) {
next = pointer.next;
} else {
break;
}
}
return head;
}
private static class Node {
int value;
Node next;
@Override
public String toString() {
return "Node{" +
"value=" + value +
", next=" + (next == null ? "" : next.value) +
'}';
}
}
private static void print (Node node) {
while (node != null) {
System.out.println(node);
node = node.next;
}
}
public static void main(String[] args) {
SwapNodeInPairs swapNodeInPairs = new SwapNodeInPairs();
Node list1 = new Node();
list1.value = 1;
Node pointer = list1;
for (int i = 2; i < 10; i++) {
Node node = new Node();
node.value = i;
pointer.next = node;
pointer = pointer.next;
}
print(list1);
System.out.println();
print(swapNodeInPairs.swap(list1));
}
}
leetcode — swap-nodes-in-pairs的更多相关文章
- LeetCode: Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [LeetCode]Swap Nodes in Pairs题解
Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- [LeetCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- leetcode—Swap Nodes in Pairs
1.题目描述 Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...
- Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- [LeetCode]Swap Nodes in Pairs 成对交换
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- [Leetcode] Swap nodes in pairs 成对交换结点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...
- LeetCode Swap Nodes in Pairs 交换结点对(单链表)
题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值. 思路:迭代法和递归法都容易写,就写个递归的了. 4ms /** * Definition for singly-linked list ...
- leetcode Swap Nodes in Pairs python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
随机推荐
- Mysql数据库 (JTree应用)
package com.databases.jtree; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt ...
- linux命令大全(1)
当用户使用linux系统时,其实在和Shell在打交道,当用户发出指令,其实先将这些指令发送给Shell, 然后由Shell将用户的指令翻译后传送给内核,再由内核来控制硬件的工作. 然后内核将硬件的工 ...
- idea连接操作数据库
场景 本文主要以DB2作为演示,其他数据库大同小异: 网上有很多推荐DB2的连接软件工具,但是因为DB2的使用场景不多,这次是在做数据资产管理的数据质量分析时使用到,在做数据交换时要在DB2中建表并同 ...
- zeromq示例代码
http://www.oschina.net/code/snippet_614253_55034 将zeromq代码使用vc集中到一个解决方案中 可编译可调调试 vc2015
- 5阶m序列
void echo32(int m) { printf("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d\n ...
- Elasticsearch下安装ik分词器
安装ik分词器(必须安装maven) 上传相应jar包 解压到相应目录 unzip elasticsearch-analysis-ik-master.zip(zip包) cp -r elasticse ...
- 学习Acegi应用到实际项目中(3)
此节将要了解的是AnonymousProcessingFilter.RememberMeProcessingFilter和LogoutFilter三个过滤器 1.AnonymousProcessing ...
- 汇编语言计算Sin,Cos,Pow函数
填了一下之前的坑.首先是一个题外话,在VS2015中默认汇编代码会使用SSE生成,如果想用FPU编译出FLD,FSTP这些指令,需要设置一下. 项目 >> 属性 >> C/C+ ...
- 第38章:MongoDB-集群--Replica Sets(副本集)---多机的搭建
①机器环境 182.48.115.236 master-node(主节点) 182.48.115.237 slave-node1(从节点) 182.48.115.238 slave- ...
- MySQL与SQL语句的操作
MySQL与SQL语句的操作 Mysql比较轻量化,企业用的是Oracle,基本的是熟悉对数据库,数据表,字段,记录的更新与修改 1. mysql基本信息 特殊数据库:information_sche ...