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,需要单独处理一下。

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     /**
11      * @param head a ListNode
12      * @oaram v1 an integer
13      * @param v2 an integer
14      * @return a new head of singly-linked list
15      */
16     public ListNode swapNodes(ListNode head, int v1, int v2) {
17         if (head == null) return null;
18         if (v1 == v2) return head;
19
20         int dummyValue = Math.abs(v1) + Math.abs(v2) + 1;
21
22         ListNode dummyHead = new ListNode(dummyValue);
23         dummyHead.next = head;
24         ListNode node1Parent = findNodeParent(dummyHead, v1);
25         ListNode node2Parent = findNodeParent(dummyHead, v2);
26         // if v1 or v2 doesn‘t exist, return
27         if (node1Parent == null || node2Parent == null) return head;
28
29         ListNode node1 = node1Parent.next;
30         ListNode node2 = node2Parent.next;
31         // special case
32         if (node1.next == node2) {
33             node1Parent.next = node2;
34             node1.next = node2.next;
35             node2.next = node1;
36         } else if (node2.next == node1) {  // special case
37             node2Parent.next = node1;
38             node2.next = node1.next;
39             node1.next = node2;
40         } else {
41             ListNode temp = node2.next;
42             node2.next = node1.next;
43             node1.next = temp;
44             node1Parent.next = node2;
45             node2Parent.next = node1;
46         }
47         return dummyHead.next;
48     }
49
50     private ListNode findNodeParent(ListNode head, int v1) {
51         while (head.next != null) {
52             if (head.next.val == v1) {
53                 return head;
54             } else {
55                 head = head.next;
56             }
57         }
58         return null;
59     }
60 }

转载请注明出处:cnblogs.com/beiyeqingteng/

时间: 2024-08-24 19:23:37

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 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 sh

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) { i

lintcode-medium-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 sh

[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 to 0 until there are no such sequences. After doing so, return the head of the final linked list.  You may return any such answer. (Note that in the example

leeetcode1171 Remove Zero Sum Consecutive Nodes from Linked List

1 """ 2 Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences. 3 4 After doing so, return the head of the final linked list. You may return any such answer. 5 6

算法题解之链表

Copy List with Random Pointers 复制带随机指针的链表 思路1:使用哈希表,需要消耗O(N)的额外空间. 1 public class Solution { 2 /** 3 * @param head: The head of linked list with a random pointer. 4 * @return: A new head of a deep copy of the list. 5 */ 6 public RandomListNode copyRa

LintCode链表题总结

由于链表本身结构的单一性,链表的题目很少会有很大的变种,基本都是围绕几个基本的考点出题目.所以链表的题目比较好掌握,但是链表的题目又不太容易一次就AC通过,由于边界情况未考虑.空指针(比如head.next不存在但是却给head.next赋值了,就会抛出nullpointer的错误).越界等边界情况,我们需要在测试用例的时候多考虑边界条件.在模拟计算的时候一定要用纸和笔把中间的操作过程给画出来,这样比较容易形成思路. 在LintCode的ladder1中,链表那一章有如下这一些题目: 此外,Li

数据结构:单向链表(Linked List)

本文来源: Linked List | Set 1 (Introduction) Linked List | Set 2 (Inserting a node) Linked List | Set 3 (Deleting a node) Find Length of a Linked List (Iterative and Recursive) (todo:在结构中保持节点数信息) Search an element in a Linked List (Iterative and Recursiv

数据结构:单向链表系列6--交换相邻两个节点1(交换数据域)

给定一个单向链表,编写函数交换相邻 两个元素 输入: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 输出: 2 -> 1 -> 4 -> 3 -> 6 -> 5 -> 7 输入: 1 -> 2 -> 3 -> 4 -> 5 -> 6 输出: 2 -> 1 -> 4 -> 3 -> 6 -> 5 通过观察发现:当输入的与元素个数是单数的时候,最后一位不参与交换