Nothing special. Just take care of corner cases.

  1. class Solution {
  2. public:
  3. /**
  4. * @param head a ListNode
  5. * @oaram v1 an integer
  6. * @param v2 an integer
  7. * @return a new head of singly-linked list
  8. */
  9. ListNode* swapNodes(ListNode* head, int v1, int v2)
  10. {
  11. if(!head) return head;
  12.  
  13. ListNode *p1 = nullptr, *p1p = nullptr, *p1n = nullptr;
  14. ListNode *p2 = nullptr, *p2p = nullptr, *p2n = nullptr;
  15.  
  16. // Pass 1: Find nodes
  17. //
  18. ListNode *prev = nullptr, *p = head, *next = p->next;
  19. while(p)
  20. {
  21. if(p->val == v1 || p->val == v2)
  22. {
  23. if(!p1)
  24. {
  25. p1 = p;
  26. p1p = prev;
  27. p1n = next;
  28. }
  29. else
  30. {
  31. p2 = p;
  32. p2p = prev;
  33. p2n = next;
  34. }
  35. }
  36. // move on
  37. prev = p;
  38. p = next;
  39. next = next?next->next:nullptr;
  40. }// while
  41.  
  42. if(!p1 || !p2)
  43. return head;
  44.  
  45. // Step 2:
  46. //
  47. ListNode *ret = head;
  48. if(p1 == head)
  49. {
  50. ret = p2;
  51. }
  52.  
  53. if (p1n == p2) // adjacent
  54. {
  55. if(p1p)
  56. p1p->next = p2;
  57. p2->next = p1;
  58. p1->next = p2n;
  59. }
  60. else
  61. {
  62. if(p1p)
  63. p1p->next = p2;
  64. p2->next = p1n;
  65. p2p->next = p1;
  66. p1->next = p2n;
  67. }
  68.  
  69. return ret;
  70. }
  71. };

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. HTML-day-1-HTML基础知识

    HTML基础知识 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  2. JDBC接口规范

    前言 JDBC(JavaDatabase Connectivity)表示Java查询引擎连接,由一组用Java编程语言编写的类和接口组成.JDBC为Java程序访问关系型查询引擎提供了编程接口,为查询 ...

  3. USB电源管理

    在USB总线接口协议中,由于涉及电源供电,因此协议中规定了完整的电源管理方案.通过USB电源管理可以实现USB设备的激活.挂起.空闲和睡眠等,从而降低无效的功率消耗,实现系统电源的有效使用和合理分配. ...

  4. JQuery中操作Css样式

    //1.获取和设置样式 $("#tow").attr("class")获取ID为tow的class属性 $("#two").attr(&qu ...

  5. Javascript模块化编程(二):AMD规范【转】

    作者: 阮一峰 日期: 2012年10月30日 这个系列的第一部分介绍了Javascript模块的基本写法,今天介绍如何规范地使用模块. (接上文) 七.模块的规范 先想一想,为什么模块很重要? 因为 ...

  6. java窗口添加背景

    1.import javax.swing.ImageIcon; 2.import javax.swing.JFrame; 3.import javax.swing.JLabel; 4.import j ...

  7. UVa 442 矩阵链乘(栈)

    Input Specification Input consists of two parts: a list of matrices and a list of expressions. The f ...

  8. linux 命令查看CPU和内存信息

    几个cpu more /proc/cpuinfo |grep "physical id"|uniq|wc -l 每个cpu是几核(假设cpu配置相同) more /proc/cpu ...

  9. js获取客户端操作系统

    function detectOS() { var sUserAgent = navigator.userAgent; var isWin = (navigator.platform == " ...

  10. How to Configure the Gradient Boosting Algorithm

    How to Configure the Gradient Boosting Algorithm by Jason Brownlee on September 12, 2016 in XGBoost ...