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 ...
随机推荐
- (转)兼容主流浏览器的CSS透明代码
透明往往能产生不错的网页视觉效果下面是兼容主流浏览器的CSS透明代码.transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; - ...
- Log4j(1.2.17) - hello world
1. Maven 依赖 <dependencies> <dependency> <groupId>log4j</groupId> <artifac ...
- Notepad++在编程使用时的小技巧
http://blog.csdn.net/freewaywalker/article/details/8010790 为了编程时更快捷和适应个人习惯,我们可以对Notepad++进行一系列的设置,这当 ...
- oracle 触发器number判断空值,:NEW赋值,for each row,sql变量引号,to_date,to_char
1.number类型在库中可能存在null这种数据 判断是否为空时要用如下: IF(nvl(:NEW.BACAH,0) <>0) 不能用IF(BACAH IS NOT NULL) 2. 2 ...
- JavaScript Boolean(布尔) 对象
创建 Boolean 对象 Boolean 对象代表两个值:"true" 或者 "false" 下面的代码定义了一个名为 myBoolean 的布尔对象: va ...
- wpf 窗体中显示当前系统时间
先看一下效果: 这其实是我放置了两个TextBlock,上面显示当前的日期,下面显示时间. 接下来展示一下代码: 在XAML中: <StackPanel Width="205" ...
- VirtualBox 中的UBUNTU和java环境的配置以及各种常用说明
本来是要ubuntu下学习C++的,但是又懒的用高手们推荐的各种记事本级的操作,所以要用IDE.(我用IDE我自豪,人类的进化就是建立在工具的使用这个基础之上的.)我选用了oracle的netbean ...
- login-登录
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...
- 通用方法解决dedecms导航调用二级、三级栏目菜单
博客之前做网站的时候经常会遇到二级菜单.三级菜单.了解dede的人都知道从5.5版本开始都有二级菜单的调用方法了,网上也有不少的教程文章.不过这个调用需要修改dede源码的二级菜单样式.个人感觉不是很 ...
- for循环,列表和格式化输出
一:for 循环 1. 简单的说如果让你输出1到100之间的整数,用while该怎么实现呢? i= : print(i) i+= 看着是不是只有4行,但是有没有更加简单的办法,不妨我 ...