【LeetCode】Swap Nodes in Pairs (3 solutions)

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, only nodes itself can be changed.

解法一:递归

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *swapPairs(ListNode *head) {
        if(head == NULL || head->next == NULL)
            return head;
        ListNode* newhead = head->next;
        head->next = swapPairs(newhead->next);
        newhead->next = head;
        return newhead;
    }
};

解法二:Reverse Nodes in k-Group令k=2

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *swapPairs(ListNode *head) {
        return reverseKGroup(head,2);
    }
    ListNode *reverseKGroup(ListNode *head, int k) {
        ListNode* newhead = new ListNode(-1);
        ListNode* tail = newhead;
        ListNode* begin = head;
        ListNode* end = begin;
        while(true)
        {
            int count = k;
            while(count && end != NULL)
            {
                end = end->next;
                count --;
            }
            if(count == 0)
            {//reverse from [begin, end)
                stack<ListNode*> s;
                while(begin != end)
                {
                    s.push(begin);
                    begin = begin->next;
                }
                while(!s.empty())
                {
                    ListNode* top = s.top();
                    s.pop();
                    tail->next = top;
                    tail = tail->next;
                }
            }
            else
            {//leave out
                tail->next = begin;
                break;
            }
        }
        return newhead->next;
    }
};

解法三:

每对结点两两交换后,返回给前一个结点进行连接。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *swapPairs(ListNode *head) {
        ListNode* newhead = new ListNode(-1);
        newhead->next = head;
        ListNode* cur = newhead;
        while(cur->next != NULL && cur->next->next != NULL)
        {
            cur->next = reverse(cur->next, cur->next->next);
            cur = cur->next->next;
        }
        return newhead->next;
    }
    ListNode* reverse(ListNode* n1, ListNode* n2)
    {//swap two adjacent nodes, the latter is returned
        n1->next = n2->next;
        n2->next = n1;
        return n2;
    }
};

时间: 2024-08-04 14:36:45

【LeetCode】Swap Nodes in Pairs (3 solutions)的相关文章

【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,

【LeetCode】Swap Nodes in Pairs 链表指针的应用

题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pairs * 题目:输入一个链表,要求将链表每相邻的两个节点交换位置后输出 * 思路:遍历一遍就可以,时间复杂度O(n) * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * Li

【Leetcode】Swap Nodes in Pairs in JAVA 难得的一次写对不带改的。。附赠测试程序like always

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】【Medium】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

【leetcode】reverse Nodes in k-groups

问题: 给定一个链表的头指针,以及一个整数k,要求将链表按每k个为一组,组内进行链表逆置.少于k个的部分不做处理. 分析: 个人觉得问题的重点是熟悉链表的就地逆置操作,就是头插法.其他的考察点如果还有的话,就的细心程度. 实现: void reverseList(ListNode *&pre, ListNode *head) { ListNode *tail = NULL; while (head) { ListNode* next = head->next; head->next =

[LeetCode] 024. Swap Nodes in Pairs (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 024. Swap Nodes in Pairs (Medium) 链接: 题目:https://oj.leetcode.com/problems/swap-nodes-in-pairs/ 代码(github):https://github.com/illuz/leetcode 题意: 把一个链表中的每一对节点对换

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 the

LeetCode 024 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 th

leetCode 24. 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