Java for LeetCode 024 Swap Nodes in Pairs
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.
解题思路:
感觉和前几题很类似,直接实现即可,JAVA代码如下:
public ListNode swapPairs(ListNode head) {
ListNode result = new ListNode(0), index = result;
if (head == null)
return result.next;
while (head.next != null) {
index.next = new ListNode(head.next.val);
index = index.next;
index.next = new ListNode(head.val);
index = index.next;
head = head.next.next;
if (head == null)
return result.next;
}
index.next = new ListNode(head.val);
return result.next;
}
Java for LeetCode 024 Swap Nodes in Pairs的更多相关文章
- LeetCode 024 Swap Nodes in Pairs
题目描述:Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- LeetCode 024 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 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- 【LeetCode】Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- Java [leetcode 24]Swap Nodes in Pairs
题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...
- 【LeetCode】024. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
随机推荐
- 【poj1182】 食物链
http://poj.org/problem?id=1182 (题目链接) 题意 中文题 Solution 带权并查集. 神犇博客,秒懂 fa记录父亲,r记录与父亲的关系.%3运用的很巧妙. 代码 / ...
- poj 3734 矩阵快速幂+YY
题目原意:N个方块排成一列,每个方块可涂成红.蓝.绿.黄.问红方块和绿方块都是偶数的方案的个数. sol:找规律列递推式+矩阵快速幂 设已经染完了i个方块将要染第i+1个方块. a[i]=1-i方块中 ...
- [NOIP2009] 提高组 洛谷P1073 最优贸易
题目描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分 为双向通行的道路 ...
- 使用U盘安装mint
系统坏了,重新装的时候,硬盘甚至都没法格式化...所以,狠狠心买了块固态硬盘,123G,威刚. 想自己装Linux系统,这样用起来更方便一点,不用装虚拟机,然后再跑linux什么的.最后选了mint. ...
- gotoTop返回顶部 JS
方法: 1.首先在body添加一个标签,在一个页面添加,其它页面也会生效. <body> <a name="top"> 2.然后在页脚添加一个链接 < ...
- 用VSCode写python的正确姿势
最近在学习python,之前一直用notepad++作为编辑器,偶然发现了VScode便被它的颜值吸引.用过之后发现它启动快速,插件丰富,下载安装后几乎不用怎么配置就可以直接使用,而且还支持markd ...
- iOS应用开发最佳实践系列一:编写高质量的Objective-C代码
本文由海水的味道编译整理,转载请注明译者和出处,请勿用于商业用途! 点标记语法 属性和幂等方法(多次调用和一次调用返回的结果相同)使用点标记语法访问,其他的情况使用方括号标记语法. 良好的 ...
- ExtJS入门教程05,grid的异步加载数据
上一篇演示了extjs grid的基本用法,并加载了本地数据.今天我们将演示如何加载异步数据. 所谓异步,就是通过ajax的方式将服务器端的数据加载到我们的grid中.为了提供数据,我们先定义一个数据 ...
- node-js访问rest api的方法
//npm install node-rest-client --save-dev var Client = require('node-rest-client').Client function l ...
- 人工神经网络(ANN)
参考资料:http://www.cnblogs.com/subconscious/p/5058741.html 从函数上来看,神经网络是回归方程的级联叠加,用来逼近目标函数的,本质是一种模拟特征与目标 ...