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的更多相关文章

  1. [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 ...

  2. LintCode "Swap Two Nodes in Linked List"

    Nothing special. Just take care of corner cases. class Solution { public: /** * @param head a ListNo ...

  3. 【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 ...

  4. leeetcode1171 Remove Zero Sum Consecutive Nodes from Linked List

    """ Given the head of a linked list, we repeatedly delete consecutive sequences of no ...

  5. 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 preSum + 字典 日期 题目地址:https:/ ...

  6. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  7. 算法与数据结构基础 - 链表(Linked List)

    链表基础 链表(Linked List)相比数组(Array),物理存储上非连续.不支持O(1)时间按索引存取:但链表也有其优点,灵活的内存管理.允许在链表任意位置上插入和删除节点.单向链表结构一般如 ...

  8. elr_memory_pool详解

    Preface Usually, memory allocation of OS is fast, especially the computer has just started. But over ...

  9. ※数据结构※→☆线性表结构(list)☆============单向循环链表结构(list circular single)(四)

    循环链表是另一种形式的链式存贮结构.它的特点是表中最后一个结点的指针域指向头结点,整个链表形成一个环. 单循环链表——在单链表中,将终端结点的指针域NULL改为指向表头结点或开始结点即可. 循环链表的 ...

随机推荐

  1. sublime text 删除安装的插件

    ctrl+shift+p输入remove package选择要删掉的插件即可

  2. Active-MQ的安装

    (1)首先就是下载软件 wget http://archive.apache.org/dist/activemq/apache-activemq/5.9.0/apache-activemq-5.9.0 ...

  3. iOS不得姐项目--推荐关注模块(一个控制器控制两个tableView),数据重复请求的问题,分页数据的加载,上拉下拉刷新(MJRefresh)

    一.推荐关注模块(一个控制器控制两个tableView) -- 数据的显示 刚开始加载数据值得注意的有以下几点 导航控制器会自动调整scrollView的contentInset,最好是取消系统的设置 ...

  4. Ajax实现联动效果

    用到了地区的数据库 html代码: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="s ...

  5. Spring-涉及到的设计模式汇总

    1. 简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. 简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类. ...

  6. Spring-事物传播行为

    spring事物的传播属性(7种) REQUIRED(默认) 业务方法需要在一个容器里运行.如果方法运行时,已经处在一个事务中,那么加入到这个事务,否则自己新建一个新的事务. 存在事物,则使用当前事物 ...

  7. 【BZOJ-2879】美食节 最小费用最大流 + 动态建图

    2879: [Noi2012]美食节 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1366  Solved: 737[Submit][Status] ...

  8. Win7 + ubuntu14.04 双系统

    安装主要分为以下几步:一. 下载Ubuntu 14.04镜像软件:二. 制作U盘启动盘:三. 安装Ubuntu系统:四. 用EasyBCD 创建启动系统. 1. 下载 Ubuntu 14.04 直接到 ...

  9. ExtJS入门教程04,这是一个超级好用的grid

    今天进行extjs入门教程的第四篇:grid. 来一份grid尝尝 小伙伴们都知道extjs的grid功能强大,更清楚功能强大的东西用起来必然会复杂.今天我们就从最简单的grid开始讲解. 先来一个最 ...

  10. json(2)

    JSON 语法规则 JSON 语法是 JavaScript 对象表示法语法的子集. 数据在名称/值对中 数据由逗号分隔 花括号保存对象 方括号保存数组 JSON 名称/值对 JSON 数据的书写格式是 ...