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 ...
随机推荐
- @getMapping和@postMapping,@RestController
@RequestMapping 和 @GetMapping @PostMapping 区别 @GetMapping是一个组合注解,是@RequestMapping(method = Reques ...
- 模型构建<1>:模型评估-分类问题
对模型的评估是指对模型泛化能力的评估,主要通过具体的性能度量指标来完成.在对比不同模型的能力时,使用不同的性能度量指标可能会导致不同的评判结果,因此也就意味着,模型的好坏只是相对的,什么样的模型是较好 ...
- disable_functions php-fpm root
php.ini disable_functions 禁用某些函数 需要时注意打开 php-fpm 对应conf user group为root时 ERROR: [pool www] please sp ...
- [POI2012]Salaries
题目大意: 给定一棵n带权树,每个点的权值在[1,n]范围内且互不相等,并满足子结点的权值一定小于父结点. 现在已知一个包含根结点的联通块中个点的权值,求剩下哪些点的权值能够被求出,并求出这些权值. ...
- Educational Codeforces Round 13 D. Iterated Linear Function 水题
D. Iterated Linear Function 题目连接: http://www.codeforces.com/contest/678/problem/D Description Consid ...
- react父子组件数据传递
子传父 1.首先父组件设定一个自定义函数 getChildDatas = (values) => { //...业务代码 } 2.将该函数暴露在子组件的引用上 <Child getChil ...
- mySql---or和in的效率问题(和<=、>=、between之间的关系)
写在前面: 本文是直接拿取的别人的实验数据作参考,然后对数据作分析. 参考网友的测试数据结果: 在网上一直看到的是or和in的效率没啥区别,一直也感觉是这样,前几天刚好在看<mysql数据库开发 ...
- mysql数据库表迁移
@ 把老数据库中的某个表倒出成sql文件 $mysqldump -uroot -p123456 my_db > my_db.sql (输入密码) @ 在新环境中导入 $sudo apt-get ...
- CentOS6安装redmine
Author: JinDate: 20140827System: CentOS release 6.5 (Final) 参考:http://www.redmine.org/projects/redmi ...
- GoodSync 同步 对比 备份
单向/双向皆可,针对任何一侧的文件进行处理,两侧文件都进行更新 文件同步 的操作过程是:确保两处或多处包含完全一致.及时更新 的各种文件.当对某一处进行文件添加.更改或删除,则同步操作将对对应的 另一 ...