Swap Two Nodes in Linked List
Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 and v2. It's guaranteed there is no duplicate values in the linked list. If v1 or v2 does not exist in the given linked list, do nothing.
Notice
You should swap the two nodes with values v1 and v2. Do not directly swap the values of the two nodes.
Example
Given 1->2->3->4->null and v1 = 2, v2 = 4.
Return 1->4->3->2->null.
分析:
因为这题里涉及到四个nodes,node1, node1Parent, node2, node2Parent, 然后有了这四个nodes,我们就可以对它们进行换位,但是这里有一种特殊情况node1是node2的parent,或者node2是node1的parent,需要单独处理一下。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* @param head a ListNode
* @oaram v1 an integer
* @param v2 an integer
* @return a new head of singly-linked list
*/
public ListNode swapNodes(ListNode head, int v1, int v2) {
if (head == null) return null;
if (v1 == v2) return head; int dummyValue = Math.abs(v1) + Math.abs(v2) + ; ListNode dummyHead = new ListNode(dummyValue);
dummyHead.next = head;
ListNode node1Parent = findNodeParent(dummyHead, v1);
ListNode node2Parent = findNodeParent(dummyHead, v2);
// if v1 or v2 doesn't exist, return
if (node1Parent == null || node2Parent == null) return head; ListNode node1 = node1Parent.next;
ListNode node2 = node2Parent.next;
// special case
if (node1.next == node2) {
node1Parent.next = node2;
node1.next = node2.next;
node2.next = node1;
} else if (node2.next == node1) { // special case
node2Parent.next = node1;
node2.next = node1.next;
node1.next = node2;
} else {
ListNode temp = node2.next;
node2.next = node1.next;
node1.next = temp;
node1Parent.next = node2;
node2Parent.next = node1;
}
return dummyHead.next;
} private ListNode findNodeParent(ListNode head, int v1) {
while (head.next != null) {
if (head.next.val == v1) {
return head;
} else {
head = head.next;
}
}
return null;
}
}
转载请注明出处:cnblogs.com/beiyeqingteng/
Swap Two Nodes in Linked List的更多相关文章
- [LintCode] Swap Two Nodes in Linked List 交换链表中的两个结点
Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 a ...
- LintCode "Swap Two Nodes in Linked List"
Nothing special. Just take care of corner cases. class Solution { public: /** * @param head a ListNo ...
- 【leetcode】1171. Remove Zero Sum Consecutive Nodes from Linked List
题目如下: Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum ...
- leeetcode1171 Remove Zero Sum Consecutive Nodes from Linked List
""" Given the head of a linked list, we repeatedly delete consecutive sequences of no ...
- 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 preSum + 字典 日期 题目地址:https:/ ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 算法与数据结构基础 - 链表(Linked List)
链表基础 链表(Linked List)相比数组(Array),物理存储上非连续.不支持O(1)时间按索引存取:但链表也有其优点,灵活的内存管理.允许在链表任意位置上插入和删除节点.单向链表结构一般如 ...
- elr_memory_pool详解
Preface Usually, memory allocation of OS is fast, especially the computer has just started. But over ...
- ※数据结构※→☆线性表结构(list)☆============单向循环链表结构(list circular single)(四)
循环链表是另一种形式的链式存贮结构.它的特点是表中最后一个结点的指针域指向头结点,整个链表形成一个环. 单循环链表——在单链表中,将终端结点的指针域NULL改为指向表头结点或开始结点即可. 循环链表的 ...
随机推荐
- 【BZOJ 3674】可持久化并查集加强版&【BZOJ 3673】可持久化并查集 by zky 用可持久化线段树破之
最后还是去掉异或顺手A了3673,,, 并查集其实就是fa数组,我们只需要维护这个fa数组,用可持久化线段树就行啦 1:判断是否属于同一集合,我加了路径压缩. 2:直接把跟的值指向root[k]的值破 ...
- 【CodeForces 622A】Infinite Sequence
题意 一个序列是, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5....这样排的,求第n个是什么数字. 分析 第n个位置属于1到k,求出k,然后n-i*(i-1)/ ...
- 第六节 JBPM版本控制以及Token对象
1.JBPM版本 2.Token 3.流程上下文
- NOI题库--图论 宗教信仰
1526:宗教信仰 总时间限制: 5000ms 内存限制: 65536kB 描述 世界上有许多宗教,你感兴趣的是你学校里的同学信仰多少种宗教. 你的学校有n名学生(0 < n <= 500 ...
- 浅议SNMP安全、SNMP协议、网络管理学习
相关学习资料 tcp-ip详解卷1:协议.pdf(重点看25章SNMP部分) http://www.rfc-editor.org/rfc/rfc1213.txt http://www.rfc-edit ...
- WWDC2014总结---For产品经理们
一年一度的苹果开发者大会WWDC2014,在北京时间6月3日凌晨1点开始了,苹果发布了iOS8.OSX10.10等,苹果比以前更加开放了,网上东西很多很杂,但缺少从产品开发角度来梳理的文章. 我根据这 ...
- Eclipse开发Android程序如何在手机上运行
android开发不论是在真机上调试还是最终发布到真机上都非常简单,过程如下: 1.安装usb驱动 手机要能与电脑相连,当然要安驱动了.效果就是你插入手机,电脑显示驱动已识别.驱动安装的官方教程:ht ...
- Java,PostgreSQL时间范围查询
遇到一坑:对于如下代码 select * from order_mileagefuel where date > '2015-11-1' and date< '2015-11-5' 在Po ...
- Unity-Tween
1.GoKit 免费开源 AssetStore:https://www.assetstore.unity3d.com/en/#!/content/3663 下载地址:https://github.co ...
- PHP中的替代语法(转)
我们经常在wordpress一类博客程序的模板里面看到很多奇怪的PHP语法,比如: <?php if(empty($GET_['a'])): ?> <font color=" ...