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. oledb 写入 office2010 以及发布到iis 遇到的奇怪问题总结

    这段时间在做Excel 导出升级,把之前的office2003 升级到 2010导出. 一 利用oledb 写入 Excel 2010 的时候,怎么也打不开文件,最后调查的原因是 需要修改连接字符串: ...

  2. This application failed to start because it could not find or load the Qt platform plugin “windows”错误解决方法

    这是一个困扰我很久的问题,关于Qt下生成的exe文件在没有安装Qt的机器上无法运行的问题.Qt是编写C++图形界面的一个很好工具,比MFC来的直观.可是,Qt的安装却是一个让人头疼的事情.早在上个学期 ...

  3. ntpdate:no server suitable for synchronization found

    Question: 在使用ntpdate同步时间时,出现了no server suitable for synchronization found的报错. 通过ntpdate -d s2m.time. ...

  4. POJ 1523 SPF(寻找关节点)

                                                                         SPF Time Limit: 1000MS   Memory ...

  5. JAVA条件语句

    1.if if (判断条件) { 条件成立时执行的代码 } 2.if..else if (判断条件) { 条件成立时执行的代码 } else { 条件不成立时执行的代码 } 3.多重if if (条件 ...

  6. 黑马程序员——JAVA基础之Collections和Arrays,数组集合的转换

    ------- android培训.java培训.期待与您交流! ---------- 集合框架的工具类:        Collections : 集合框架的工具类.里面定义的都是静态方法. Col ...

  7. 利用 t-SNE 高维数据的可视化

    利用 t-SNE 高维数据的可视化  具体软件和教程见: http://lvdmaaten.github.io/tsne/  简要介绍下用法: % Load data load ’mnist_trai ...

  8. GDB动态库搜索路径

    当GDB无法显示so动态库的信息或者显示信息有误时,通常是由于库搜索路径错误导致的,可使用set sysroot.set solib-absolute-prefix.set solib-search- ...

  9. Win10系统下安装Tensorflow

    TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,具备良好的灵活性和拓展性. 过去TensorFlow只支持Linux和Mac OS,而没有提供windows的支持. ...

  10. 代码里面执行bat

    public static void executeBat(String path) {        try {            File file = new File(path);     ...