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 ...
随机推荐
- CentOS7 使用ifconfig命令 ENS33没有IP地址的解决办法
最近在研究和学习Linux操作系统,我并没有安装独立的Linux操作系统,我选择在虚拟机上安装Linux操作系统.我选择的虚拟机的版本是VMware Workstation Pro14,然后在虚拟机上 ...
- Python开发——数据类型【元祖】
元祖的定义 tu = (11,22,33,44,) print(tu) # (11, 22, 33, 44) tu = tuple((11,22,33,44,)) print(tu) # (11, 2 ...
- 防火墙/IDS测试工具Ftester
防火墙/IDS测试工具Ftester FTester 全称Firewall Tester,是一个用来测试防火墙的过滤策略和入侵检测(IDS)能力的工具.这个工具主要是有两个perl的脚本组成: 1. ...
- JavaScript 高阶函数
高阶函数的英文叫Higher-order function ,什么是高阶函数呢>? JavaScript的函数其实都指向某个变量.既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接 ...
- 树上差分——点差分裸题 P3128 [USACO15DEC]最大流Max Flow
讲解: https://rpdreamer.blog.luogu.org/ci-fen-and-shu-shang-ci-fen #include <bits/stdc++.h> #def ...
- (22)Embrace the near win
https://www.ted.com/talks/sarah_lewis_embrace_the_near_win/transcript?referrer=playlist-talks_to_get ...
- qhfl-4 注册-登录-认证
认证 任何的项目都需要认证,当用户输入了用户名和密码,验证通过,代表用户登录成功 那HTTP请求是无状态的,下次这个用户再请求,是不可能识别这个用户是否登录的 就要有自己的方式来实现这个认证,用户登录 ...
- noip第14课资料
- 六.使用python操作mysql数据库
数据库的安装和连接 pymysql的安装 pip install PyMySQL python连接数据库 import pymysql db = pymysql.connec ...
- HTTPS抓包之Charles
这里对HTTP请求的抓包操作不做讲解了,只讲解HTTPS的抓包要进行的操作. [说明]:下面以MAC电脑示例,Windows版本可参考:http://weibo.com/ttarticle/p/sho ...