Lintcode511 Swap Two Nodes in Li

2018-04-12  本文已影响0人  程风破浪会有时

【题目描述】

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/solutions/swap-two-nodes-in-linked-list/

上一篇 下一篇

猜你喜欢

热点阅读