Nothing special. Just take care of corner cases.

class Solution {
public:
/**
* @param head a ListNode
* @oaram v1 an integer
* @param v2 an integer
* @return a new head of singly-linked list
*/
ListNode* swapNodes(ListNode* head, int v1, int v2)
{
if(!head) return head; ListNode *p1 = nullptr, *p1p = nullptr, *p1n = nullptr;
ListNode *p2 = nullptr, *p2p = nullptr, *p2n = nullptr; // Pass 1: Find nodes
//
ListNode *prev = nullptr, *p = head, *next = p->next;
while(p)
{
if(p->val == v1 || p->val == v2)
{
if(!p1)
{
p1 = p;
p1p = prev;
p1n = next;
}
else
{
p2 = p;
p2p = prev;
p2n = next;
}
}
// move on
prev = p;
p = next;
next = next?next->next:nullptr;
}// while if(!p1 || !p2)
return head; // Step 2:
//
ListNode *ret = head;
if(p1 == head)
{
ret = p2;
} if (p1n == p2) // adjacent
{
if(p1p)
p1p->next = p2;
p2->next = p1;
p1->next = p2n;
}
else
{
if(p1p)
p1p->next = p2;
p2->next = p1n;
p2p->next = p1;
p1->next = p2n;
} return ret;
}
};

LintCode "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. 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 ...

  3. [LintCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head.   Example Given 1->2-> ...

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

  5. leeetcode1171 Remove Zero Sum Consecutive Nodes from Linked List

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

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

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

  7. [LintCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

    Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...

  8. lintcode 中等题: reverse linked list II 翻转链表II

    题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...

  9. lintcode 中等题:Palindrome Linked List 回文链表

    题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的 ...

随机推荐

  1. 开源 VS 商业,消息中间件你不知道的那些事

    11月23日,新炬网络中间件技术专家刘拓老师在DBA+社群中间件用户组进行了一次主题为“开源 VS 商业,消息中间件你不知道的那些事”的线上分享.小编特别整理出其中精华内容,供大家学习交流. 嘉宾简介 ...

  2. 蓝牙(Profile)构成

    蓝牙剖面(Profile)构成 蓝牙剖面(Profile)及其相互关系 蓝牙SIG定义13种蓝牙剖面及其相互关系如下图: 一个剖面若直接或间接采用了另一个剖面的部分或全部功能则称该剖面依赖于另一剖面. ...

  3. 学习iOS笔记第一天的C语言学习记录

    c语言基础学习 int num1 = 15; int num2 = 5; int temp = 0; //先把num1放到temp里 temp = num1; //先把num2放到num1里 num1 ...

  4. lecture7-序列模型及递归神经网络RNN(转载)

    Hinton 第七课 .这里先说下RNN有recurrent neural network 和 recursive neural network两种,是不一样的,前者指的是一种人工神经网络,后者指的是 ...

  5. XUtils解析

    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);      ...

  6. Ubuntu 安装软件的命令

    Ubuntu 安装软件的命令 安装flashplayer sudo apt-get install flashplugin-installer 安装百度云客户端 软件在百度云盘里面 安装必要的开发环境 ...

  7. tomcat 启动时内存溢出

    在tomcat_home/bin目录下找到catalina.bat,用文本编辑器打开,加上下面一行: set JAVA_OPTS= -Xms1024M -Xmx1024M -XX:PermSize=2 ...

  8. Apache配置站点根目录、用户目录及页面访问属性

    一.配置站点根目录及页面访问属性 DocumentRoot "/www/htdoc" <Directory "/www/htdoc"> Option ...

  9. 1-3-2 Windows应用程序常用消息

    主要内容:介绍Windows编程中常用的消息 1.WM_LBUTTONDOWN产生单击鼠标左键的消息 lParam: 低字节包含当前光标的X坐标值 X = LOWORD(lParam); 高字节包含当 ...

  10. Android Studio导入Project的方法

    Android Studio到现在已经发展到0.8+的版本了,最近也在试着使用它,原因是多方面的,一个毕竟是未来的趋势,二则是github上越来越多的大牛开源项目都是基于Android Studio的 ...