LeetCode(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 values in the list, only nodes itself can be changed.

分析

如示例所示,给定一个链表,要求交换链表中相邻两个节点。

对于此题的程序实现,必须注意的是指针的判空,否则,一不注意就会出现空指针异常。

AC代码

/**
 * 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   *p = head , *q = p->next;
        //首先交换头两个结点,同时保存q后结点
        ListNode *r = q->next;

        head = q;
        p->next = r;
        q->next = p;
        if (r && r->next)
        {
            ListNode *pre = p;
            p = p->next;
            q = p->next;

            while (q)
            {
                //保存q结点后结点
                ListNode *r = q->next;

                pre->next = q;
                p->next = r;
                q->next = p;
                if (r && r->next)
                {
                    pre = p;
                    p = r;
                    q = p->next;
                }
                else{
                    break;
                }
            }
        }
        return head;
    }
};

GitHub测试程序源码

时间: 2024-08-04 09:22:54

LeetCode(24) Swap Nodes in Pairs的相关文章

LeetCode(24) - Swap Nodes in Pairs

题目要求很简单,就是把list里面的node两两互换. 当做比较复杂的LinkedList的题目的时候,最好是在草稿纸上画一画,走一遍流程,凭空想很容易会出错.这一题时LeetCode 25的k=2特殊例子,所以要简单很多.用两个node,一前一后,然后两两交换就好,细节上注意的是交换后两个node前面和后面是否各自连接上.最好自己在纸上走一遍,防止出错. 代码如下: 1 /** 2 * Definition for singly-linked list. 3 * public class Li

leetcode第23题--Swap Nodes in Pairs

Problem: 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

LeetCode(25)Reverse Nodes in k-Group

题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only

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

24. Swap Nodes in Pairs(js)

24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. You may not modify the values in the list's nodes, only nodes itself may be changed. Example: Given 1->2->3->4, you should return the list as 2->1-

leetcode_1题——Swap Nodes in Pairs(线性表的链式存储)

Swap Nodes in Pairs Total Accepted: 45110 Total Submissions: 138992My Submissions Question Solution 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-

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

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