Swap Nodes in Pairs leetcode java
题目:
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.http://i.cnblogs.com/EditPosts.aspx?opt=1
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
题解:
这道题考察了基本的链表操作,注意当改变指针连接时,要用一个临时指针指向原来的next值,否则链表丢链,无法找到下一个值。
本题的解题方法是:
需要运用fakehead来指向原指针头,防止丢链,用两个指针,ptr1始终指向需要交换的pair的前面一个node,ptr2始终指向需要交换的pair的第一个node。
然后就是进行链表交换。
需要用一个临时指针nextstart, 指向下一个需要交换的pair的第一个node,保证下一次交换的正确进行。
然后就进行正常的链表交换,和指针挪动就好。
当链表长度为奇数时,ptr2.next可能为null;
当链表长度为偶数时,ptr2可能为null。
所以把这两个情况作为终止条件,在while判断就好,最后返回fakehead.next。
代码如下:
1 public ListNode swapPairs(ListNode head) {
2 if(head == null || head.next == null)
3 return head;
4
5 ListNode fakehead = new ListNode(-1);
6 fakehead.next = head;
7
8 ListNode ptr1 = fakehead;
9 ListNode ptr2 = head;
while(ptr2!=null && ptr2.next!=null){
ListNode nextstart = ptr2.next.next;
ptr2.next.next = ptr2;
ptr1.next = ptr2.next;
ptr2.next = nextstart;
ptr1 = ptr2;
ptr2 = ptr2.next;
}
return fakehead.next;
}
Reference://http://gongxuns.blogspot.com/2012/12/leetcodeswap-nodes-in-pairs.html
Swap Nodes in Pairs leetcode java的更多相关文章
- 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
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 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 Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation
1. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For e ...
- 【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 ...
随机推荐
- 【知了堂学习笔记】java web 简单的登录
最近皮皮潇在学习java web,刚接触了简单的东西,所以今天给大家带来一个简单的登录实现. 页面: 页面代码: <%@ page language="java" conte ...
- 一文搞定 Mybatis 的应用
Mybatis 介绍 Mybatis 是一个开源的持久层框架,原来叫 ibatis ,它对 jdbc 操作数据库的过程进行了封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动. ...
- 详细介绍如何在Eclipse中使用SVN
一.在Eclipse中下载安装Subclipse插件 1 打开eclipse,在Help菜单中找到marketPlace,点击进入. 2 在搜索框Find中输入subclipse,点击右边的Go按 ...
- splice() 的用法
splice splice()方法是修改Array的“万能方法”,它可以从指定的索引开始删除若干元素,然后再从该位置添加若干元素: var arr = ['Microsoft', 'Apple', ' ...
- [BZOJ3779]重组病毒(LCT+DFS序线段树)
同[BZOJ4817]树点涂色,只是多了换根操作,分类讨论下即可. #include<cstdio> #include<algorithm> #define lc ch[x][ ...
- BZOJ 2118 墨墨的等式(最短路)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2118 [题目大意] 求a1x1+a2y2+…+anxn=B在B的取值范围,有多少B可以 ...
- 最新OFFICE 0day漏洞分析
漏洞概述 fireeye最近公布了一个OFFICE 0day,在无需用户交互的情况下,打开word文档就可以通过hta脚本执行任意代码.经过研究发现,此漏洞的成因主要是word在处理内嵌OLE2LIN ...
- python开发_tkinter_图形随鼠标移动
做这个东西的时候,灵感源自于一个js效果: 两个眼睛随鼠标移动而移动 运行效果: =============================================== 代码部分: ===== ...
- Redis系列之(一):10分钟玩转Redis
1. Redis介绍 Redis是一个开源的使用ANSI C语言编写.基于内存的Key-Value数据库. 它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集 ...
- servlet注入service业务bean
项目中用到spring容器来管理业务bean,在servlet中就收到前台传递来的请求参数后,调用业务bean,老是出错 部门代码如下 <span style="font-size:1 ...