找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2860|回复: 0

[代码与实例] Lintcode511 Swap Two Nodes in Linked List solution 题解

4

主题

4

帖子

4

积分

贫民

积分
4
rockerrrr 发表于 2018-7-30 10:23:11 | 显示全部楼层 |阅读模式
【题目描述】
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.
给你一个链表以及两个权值v1和v2,交换链表中权值为v1和v2的这两个节点。保证链表中节点权值各不相同,如果没有找到对应节点,那么什么也不用做。
注意事项:你需要交换两个节点而不是改变节点的权值

【题目链接】
www.lintcode.com/en/problem/swap-two-nodes-in-linked-list/

【题目解析】
遍历,找到nodeV1和nodeV2,交换即可。
保证noveV1一定在nodeV2之前,然后分两种情况调换:
1、noveV1和nodeV2相邻;2、不相邻

【参考答案】
www.jiuzhang.com/soluti**/swap-two-nodes-in-linked-list/


简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表