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)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的 ...
随机推荐
- wddm 部署问题解决
在把wddm部署到一台老的服务上的 windows server 2003 上时遇到了问题,之前也在 windows server 2003 上装过,但是并没有遇到问题,估计和服务器比较老有关系. 问 ...
- debug进入线程中
今天debug调试程序时,怎么也进入不了线程中,f5直接进源码,f6直接跳过了,后来把断点打在线程的Run()方法里面,按f8(可能要多按几次)就可以了.
- C# 读取文本文档(转)
1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出. byte[] byData = ...
- lanuchy快捷操作
down arrow: display history shift+delete: remove the item from the distory
- windows服务与桌面交互
最近做服务与桌面交互的尝试,结果发现windows service 无法和桌面程序进行交互,后来在网上查资料,发现了下面的连接 http://www.cnblogs.com/gnielee/archi ...
- 66. Plus One
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- tyvj 1057 dp 变形背包
P1057 金明的预算方案 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2006 提高组 第二道 描述 金明今天很开心,家里购置的新房就要领钥匙了 ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- JavaWeb学习记录(六)——用户登录功能之Session与验证码验证功能的实现
一.产生验证码的工具类 package blank.util; import java.awt.Color;import java.awt.Graphics;import java.awt.image ...
- HDU 1087 Super Jumping! Jumping! Jumping
HDU 1087 题目大意:给定一个序列,只能走比当前位置大的位置,不可回头,求能得到的和的最大值.(其实就是求最大上升(可不连续)子序列和) 解题思路:可以定义状态dp[i]表示以a[i]为结尾的上 ...