LintCode "Swap Two Nodes in Linked List"
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"的更多相关文章
- [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 ...
- 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 Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2-> ...
- 【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] Flatten Binary Tree to Linked List 将二叉树展开成链表
Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...
- lintcode 中等题: reverse linked list II 翻转链表II
题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...
- lintcode 中等题:Palindrome Linked List 回文链表
题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的 ...
随机推荐
- HTML-day-1-HTML基础知识
HTML基础知识 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...
- JDBC接口规范
前言 JDBC(JavaDatabase Connectivity)表示Java查询引擎连接,由一组用Java编程语言编写的类和接口组成.JDBC为Java程序访问关系型查询引擎提供了编程接口,为查询 ...
- USB电源管理
在USB总线接口协议中,由于涉及电源供电,因此协议中规定了完整的电源管理方案.通过USB电源管理可以实现USB设备的激活.挂起.空闲和睡眠等,从而降低无效的功率消耗,实现系统电源的有效使用和合理分配. ...
- JQuery中操作Css样式
//1.获取和设置样式 $("#tow").attr("class")获取ID为tow的class属性 $("#two").attr(&qu ...
- Javascript模块化编程(二):AMD规范【转】
作者: 阮一峰 日期: 2012年10月30日 这个系列的第一部分介绍了Javascript模块的基本写法,今天介绍如何规范地使用模块. (接上文) 七.模块的规范 先想一想,为什么模块很重要? 因为 ...
- java窗口添加背景
1.import javax.swing.ImageIcon; 2.import javax.swing.JFrame; 3.import javax.swing.JLabel; 4.import j ...
- UVa 442 矩阵链乘(栈)
Input Specification Input consists of two parts: a list of matrices and a list of expressions. The f ...
- linux 命令查看CPU和内存信息
几个cpu more /proc/cpuinfo |grep "physical id"|uniq|wc -l 每个cpu是几核(假设cpu配置相同) more /proc/cpu ...
- js获取客户端操作系统
function detectOS() { var sUserAgent = navigator.userAgent; var isWin = (navigator.platform == " ...
- How to Configure the Gradient Boosting Algorithm
How to Configure the Gradient Boosting Algorithm by Jason Brownlee on September 12, 2016 in XGBoost ...