Swap Nodes in Pairs——LeetCode
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.
题目大意:给定一个单链表,交换每两个元素。
解题思路:采用头插法重新构建这个链表,每插入两个就把头指针移动到最后,再插入新的节点。
Talk is cheap>>
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode res = new ListNode(0);
res = head.next;
ListNode cur = new ListNode(0);
ListNode tmp = head;
ListNode st = head;
int cnt = 1;
while (cur != null && st != null) {
tmp = st.next;
st.next = cur.next;
cur.next = st;
st = tmp;
if (cnt == 2) {
cnt = 0;
cur = cur.next.next;
}
cnt++;
}
return res;
}
Swap Nodes in Pairs——LeetCode的更多相关文章
- Swap Nodes in Pairs leetcode
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- Swap Nodes in Pairs leetcode java
题目: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...
- Swap Nodes in Pairs LeetCode题解
做完这个题目,感觉LeetCode的题目出的真好... 这种题,如果让我在面试时候纸上写代码,肯定会挂的. 我昨天晚上看的题目,昨天脑子是懵的,放下了.今天早上来做. 一开始做,提交,果然错了.写的代 ...
- 【LeetCode】Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 【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 Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 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 线性表 Swap Nodes in Pairs
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
随机推荐
- Java-分页实例
1.PageModel.java package com.javaweb; import java.util.List; public class PageModel<E> { priva ...
- java移位运算的用途
参考下面这篇文章 http://blog.csdn.net/gaowen_han/article/details/7163104 http://jinguo.iteye.com/blog/540150 ...
- android如何获取手机型号和版本号
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView ...
- oracle 导出导入数据
在window的运行中输出cmd,然后执行下面的一行代码, imp blmp/blmp@orcl full=y file=D:\blmp.dmp OK,问题解决.如果报找不到该blmp.dmp文件,就 ...
- Convention插件的使用(会涉及content目录,jsp必须放入这个下面才能映射成功基于注解的配置)
http://blog.csdn.net/zclandzzq/article/details/7107816
- iOS6以后的单个控制器横竖屏显示以及旋转屏控制技巧,附带iOS8以后显示电池状态栏
一.在应用中从竖屏模式强制转换为横屏模式 第一种方法:通过模态弹出视图的方式,使得特定ViewController坚持特定的interfaceOrientation(1)iOS6之后提供了这样一个方法 ...
- Java设计模式(学习整理)---工厂模式
1.工厂模式 1.1 为什么使用工厂模式? 因为工厂模式就相当于创建实例对象的new,我们经常要根据类Class生成实例对象,如A a=new A() 工厂模式也是用来创建实例对象的,所以以后new时 ...
- Java学习----对象与对象之间的关系
1.依赖 对象之间最弱的一种关联方式,是临时性的关联.代码中一般指由局部变量,函数参数,返回值建立的对于其他对象的调用关系. public class A { // 方法一 public void t ...
- 在php中使用Memcache
在上一篇博客中我写到了memcache的安装和管理,在这一篇博客中我将写到的是在php中如何使用memcache. 一.安装memcache扩展 首先我们通过phpinfo()函数查看一下我们当前的p ...
- Trigger model Trigger expr_id in WorkFolow
For example, suppose you want to set a Sale Order into the state "Done" once it has been s ...