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改为指向表头结点或开始结点即可. 循环链表的 ...
随机推荐
- zabbix 乱码的问题
文章转自:http://www.ttlsa.com/zabbix/zabbix-chinese-garbled-ttlsa/ 在使用zabbix的时候发现图片下方的中文都是一个个小方格 这是zabbi ...
- Opentack常用技巧
这篇博客将收集一些openstack里的使用技巧. 1. which dhcp agent is hosting the network? 2. 各个网桥port命令规则 grep for por ...
- Java基础-重写-子类重写父类中的方法后执行情况
代码 public class Test { public static void main(String[] args) { Shape shape = new Circle(); System.o ...
- 解决Oracle忘记密码问题
在使用ORACLE的过程中,会出现各种各样的问题,各种各样的错误,其中ORA-12899就是前段时间我在将数据导入到我本地机器上的时候一直出现的问题.不过还好已经解决了这个问题,现在分享一下,解决方案 ...
- EPROCESS 进程/线程优先级 句柄表 GDT LDT 页表 《寒江独钓》内核学习笔记(2)
在学习笔记(1)中,我们学习了IRP的数据结构的相关知识,接下来我们继续来学习内核中很重要的另一批数据结构: EPROCESS/KPROCESS/PEB.把它们放到一起是因为这三个数据结构及其外延和w ...
- 深入浅出Redis04使用Redis数据库(lists类型)
一 lists类型及操作 List是一个链表结构,主要功能是push,pop,获取一个范围的所有值等等,操作中key理解为链表的名字. Redis的list类型其实就是一个每个子元素都是sring类 ...
- 使用Jquery+EasyUI 进行框架项目开发案例讲解之四 组织机构管理源码分享
http://www.cnblogs.com/huyong/p/3404647.html 在上三篇文章 <使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码分享> ...
- js闭包理解
js闭包的作用是使函数外可以访问函数内部的变量,是通过 在函数内部 定义 访问函数内变量 的函数实现的,内部的一个函数产生一个闭包 function a() { var i=0; return fun ...
- 深入分析Java Web中的中文编码问题
要对Java Web项目进行编码原因: 1.在计算机中存储信息的最小单位是1个字节,即8个bit,所以能表示的字符范围是0~255个. 2.电脑需要表示的符号太多.无法用1个字节完全表示. 要解决这个 ...
- Facebook内部高效工作PPT指南
Facebook内部高效工作PPT指南 Facebook 内部分享:不论你如何富有,你都赚不到更多的时间,你也回不到过去.没有那么多的假如,只有指针滴答的时光飞逝和你应该好好把握的现在,以下25张PP ...