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 and v2. It's guaranteed there is no duplicate values in the linked list. If v1 or v2 does not exist in the given linked list, do nothing.
Notice
You should swap the two nodes with values v1 and v2. Do not directly swap the values of the two nodes.
Example
Given 1->2->3->4->null and v1 = 2, v2 = 4.
Return 1->4->3->2->null.
分析:
因为这题里涉及到四个nodes,node1, node1Parent, node2, node2Parent, 然后有了这四个nodes,我们就可以对它们进行换位,但是这里有一种特殊情况node1是node2的parent,或者node2是node1的parent,需要单独处理一下。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* @param head a ListNode
* @oaram v1 an integer
* @param v2 an integer
* @return a new head of singly-linked list
*/
public ListNode swapNodes(ListNode head, int v1, int v2) {
if (head == null) return null;
if (v1 == v2) return head; int dummyValue = Math.abs(v1) + Math.abs(v2) + ; ListNode dummyHead = new ListNode(dummyValue);
dummyHead.next = head;
ListNode node1Parent = findNodeParent(dummyHead, v1);
ListNode node2Parent = findNodeParent(dummyHead, v2);
// if v1 or v2 doesn't exist, return
if (node1Parent == null || node2Parent == null) return head; ListNode node1 = node1Parent.next;
ListNode node2 = node2Parent.next;
// special case
if (node1.next == node2) {
node1Parent.next = node2;
node1.next = node2.next;
node2.next = node1;
} else if (node2.next == node1) { // special case
node2Parent.next = node1;
node2.next = node1.next;
node1.next = node2;
} else {
ListNode temp = node2.next;
node2.next = node1.next;
node1.next = temp;
node1Parent.next = node2;
node2Parent.next = node1;
}
return dummyHead.next;
} private ListNode findNodeParent(ListNode head, int v1) {
while (head.next != null) {
if (head.next.val == v1) {
return head;
} else {
head = head.next;
}
}
return null;
}
}
转载请注明出处:cnblogs.com/beiyeqingteng/
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 ...
- LintCode "Swap Two Nodes in Linked List"
Nothing special. Just take care of corner cases. class Solution { public: /** * @param head a ListNo ...
- 【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]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 算法与数据结构基础 - 链表(Linked List)
链表基础 链表(Linked List)相比数组(Array),物理存储上非连续.不支持O(1)时间按索引存取:但链表也有其优点,灵活的内存管理.允许在链表任意位置上插入和删除节点.单向链表结构一般如 ...
- elr_memory_pool详解
Preface Usually, memory allocation of OS is fast, especially the computer has just started. But over ...
- ※数据结构※→☆线性表结构(list)☆============单向循环链表结构(list circular single)(四)
循环链表是另一种形式的链式存贮结构.它的特点是表中最后一个结点的指针域指向头结点,整个链表形成一个环. 单循环链表——在单链表中,将终端结点的指针域NULL改为指向表头结点或开始结点即可. 循环链表的 ...
随机推荐
- C/C++语言算法题——替换
[问题] Description 给定一个有限长度的非负整数序列.一次操作是指从第一个元素开始,依次把数列中的每个数替换为它右边比它小的数的个数.对该数列不断进行这个操作.总有一个时刻该数列将不再发生 ...
- OC基础--分类(category) 和 协议(protocol)
OC 中的category分类文件相当于 C#中的部分类:OC 中的protocol协议文件(本质是头文件)相当于 C#中的接口.今天就简单说明一下OC中的这两个文件. 由于视频中的Xcode版本低, ...
- 【CodeForces 567F】Mausoleum
寒假最后一题补完啦 ^∀^ 题意 1到n每个数字有两个,排成先不降后不升的序列,比如112332,并且满足k个形如 3 <= 6 代表第三个数字要≤第六个数字这样的约束要求,求有多少种排法. 分 ...
- 【POJ 2484】A Funny Game
Description Alice and Bob decide to play a funny game. At the beginning of the game they pick n(1 &l ...
- logback 项目应用
1.gradle引用: compile group: 'commons-logging', name: 'commons-logging', version: '1.1.3' compile grou ...
- ABK (枚举)
ABK Accepted : 24 Submit : 176 Time Limit : 1000 MS Memory Limit : 65536 KB 题目描述 ABK是一个比A+B还要简单 ...
- shell编程报错 [: missing `]'
NGINX_RATES=50 NGINX_BURST=3000 NGINX_PATH=/opt/srv/nginx/conf/nginx.conf BEE_PATH=/opt/srv/nginx/co ...
- 添加删除虚拟ip
添加 ip -f inet addr add 192.168.1.245/32 brd 192.168.1.245 dev ens32 删除 ip -f inet addr del 192.168 ...
- js 判断所有节假日
// JavaScript Document calendar = new Date(); month = calendar.getMonth(); date = calendar.getDate() ...
- Android 遍历界面控件
//遍历界面上的控件 fubin.pan LinearLayout sLinerLayout = (LinearLayout)findViewById(R.id.layout_scr); for (i ...