Swap Nodes in Pairs,成对儿的交换元素

问题描述:给一序列,交换每相邻的两个元素,并返回头结点。例如:1-2-3-4   返回序列2-1-4-3

算法思路:除了第一组元素,其他每次交换一对儿元素,要改变四个指针。所以,定义四个指针。其中只有两个指针是不想关,其他依赖这两个指针。

public static ListNode swapPairs(ListNode head) {
        ListNode pPrepre = null;  //节点对的前前元素
        ListNode pPre = null;     //节点对的前一个元素,依赖p
        ListNode p = head;        //要移动的节点对的第一个元素
        ListNode pNext = null;    //节点对的第二个元素,依赖p
        while (p != null && p.next != null) {
            pPre = p;
            p = p.next;
            pNext = p.next;
            if (pPre == head) {
                head = p;
            }
            if (pPrepre != null) {
                pPrepre.next = p;
            }
            p.next = pPre;
            pPre.next = pNext;

            pPrepre = pPre;//其他元素都依赖p,但pPrepre不依赖p,所以每次移动pPrepre和p
            p = pNext;
        }
        return head;
    }
时间: 2024-10-06 01:10:08

Swap Nodes in Pairs,成对儿的交换元素的相关文章

[LintCode] Swap Nodes in Pairs 成对交换节点

Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2->3->4, you should return the list as 2->1->4->3. Challenge Your algorithm should use only constant space. You may not modify the values in the lis

[LeetCode]Swap Nodes in Pairs 成对交换

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, on

[LeetCode] Swap Nodes in Pairs 成对交换节点

Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, onl

LeetCode24 Swap Nodes in Pairs 成对交换链表节点

题目: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list

Leetcode:Swap Nodes in Pairs 链表成对交换节点

Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the va

每日算法之二十二:Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, on

Leetcode-24 Swap Nodes in Pairs

#24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify t

LeetCode: Swap Nodes in Pairs 解题报告

Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the val

63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the va