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)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的 ...
随机推荐
- Cocos2d-x游戏移植到Android平台
1.所需环境支持:Android SDK.NDK.Eclipse.Cygwin.(本人所用系统WIN7-64位) (1)Cygwin的下载安装: Cygwin是Windows下的Linux模拟环境,用 ...
- Ubuntu下输入su - [root]后提示“su:认证失败”
Ubuntu下,进行用户到管理员切换时,使用命令su - 时,提示输入的是root密码,而在Ubuntu下root的密码起始是随机生成的(后续可由用户自己设置),且ubuntu下只能调用root,不能 ...
- cdoj 482 优先队列+bfs
Charitable Exchange Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Othe ...
- java如何获取当前机器ip和容器port
获取当前机器ip: private static String getIpAddress() throws UnknownHostException { InetAddress address = I ...
- JavaWeb学习记录(五)——Servlet随机产生验证码
随机产生验证码的工具类: import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImage;impo ...
- strlen() 函数
strlen() 函数通常用来计算字符串的长度,但是今天突然发现个奇怪的现象. 如下所示: #include <stdio.h> #include <stdlib.h> #in ...
- ps命令详解(转)
原文地址:http://apps.hi.baidu.com/share/detail/32573968 有时候系统管理员可能只关心现在系统中运行着哪些程序,而不想知道有哪些进程在运行.由于一个应用程序 ...
- java的nio之:java的nio系列教程之SocketChannel
Java NIO中的SocketChannel是一个连接到TCP网络套接字的通道.可以通过以下2种方式创建SocketChannel: 打开一个SocketChannel并连接到互联网上的某台服务器. ...
- gcc: multiple definition of [转]
/home/tace/openav/source/SeamlessMessage/CPaoFlt.o: In function `CPaoFlt::get_m_strPrmair() const':C ...
- Linux定时任务Crontab执行PHP脚本
http://blog.chinaunix.net/uid-7552018-id-182133.html crontab执行php脚本 http://www.jb51.net/article/2913 ...