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 ...
随机推荐
- SSH配置文件和SSM配置文件的写法
一.SSH配置文件的写法(XML版本) <util:properties id="jdbc" location="classpath:db.properties&q ...
- 【深入】java 单例模式(转)
[深入]java 单例模式 关于单例模式的文章,其实网上早就已经泛滥了.但一个小小的单例,里面却是有着许多的变化.网上的文章大多也是提到了其中的一个或几个点,很少有比较全面且脉络清晰的文章,于是,我便 ...
- 【poj1050】 To the Max
http://poj.org/problem?id=1050 (题目链接) 题意 求二维最大子矩阵 Solution 数据好像很水,N最大才100,N^4大暴力都可以随便水过. 其实有N^3的做法.枚 ...
- HD 2177(威佐夫博弈 入门)
取(2堆)石子游戏 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 【js】JSON.stringify 语法实例讲解
语法: JSON.stringify(value [, replacer] [, space]) value:是必选字段.就是你输入的对象,比如数组,类等. replacer:这个是可选的.它又分为 ...
- 删除ECSHOP后台升级提示/下载最新补丁升级提示
删除ECSHOP后台升级提示/下载最新补丁升级提示 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2014-05-30 删除最新补丁: v 2.7.3 releas ...
- --Dirring love 音乐(01背包问题)
解题思路: dp[i][j] 前 i 首歌放入 j 容量中的最大热情度. 前 i 首歌 放到 j 容量中 dp[i][j]= dp[i-1][j-m[i]]+r[i] (注意:如果 j 容量 &l ...
- MySQL中varchar类型在5.0.3后的变化
1.mysql varchar类型变化:mysql 5.0.3 之前: 0--255字节 varchar(20)中的20表示字节数,如果存放urf8编码的话只能放6个汉字. MySQL 5.0.3 之 ...
- TreeSet里面放对象,如果同时放入了父类和子类的实例对象,那比较时使用的是父类的compareTo方法,还是使用的子类的compareTo方法,还是抛异常!
/** * * @author ocq */ class Parent implements Comparable { private int age = 0; public Parent(int a ...
- 通过开源程序同时解决DNS劫持和DNS污染的问题
我们知道,某些网络运营商为了某些目的,对DNS进行了某些操作,导致使用ISP的正常上网设置无法通过域名取得正确的IP地址.常用的手段有:DNS劫持和DNS污染.关于DNS劫持和DNS污染的区别,请查找 ...