LeetCode 24 Swap Nodes in Pairs (C,C++,Java,Python)

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

Solution:

单纯的链表指针操作,没有什么好讲的,复杂度O(n)

题目大意:

给一个链表,要求将链表的奇数个节点和它后边的节点进行互换,并且不能更改节点的值(也就是不能互换节点的值),空间复杂度要求为O(1)

Java源代码(272ms):

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode p=new ListNode(0),s;
        p.next=head;
        head=p;
        while(p.next!=null && p.next.next!=null){
            s=p.next.next;
            p.next.next=s.next;
            s.next=p.next;
            p.next=s;
            p=s.next;
        }
        return head.next;
    }
}

C语言源代码(1ms)不开辟新的节点内存:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* swapPairs(struct ListNode* head) {
    struct ListNode *p=head,*s;
    if(p!=NULL && p->next!=NULL){
        s=p->next;
        p->next=s->next;
        s->next=p;
        head=s;
        while(p->next!=NULL && p->next->next!=NULL){
            s=p->next->next;
            p->next->next=s->next;
            s->next=p->next;
            p->next=s;
            p=s->next;
        }
    }
    return head;
}

C++源代码(9ms):

/**
 * 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 *p,*s;
        p=new ListNode(0);
        p->next=head;
        head=p;
        while(p->next!=NULL && p->next->next!=NULL){
            s=p->next->next;
            p->next->next=s->next;
            s->next=p->next;
            p->next=s;
            p=s->next;
        }
        return head->next;
    }
};

Python源代码(81ms):

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param {ListNode} head
    # @return {ListNode}
    def swapPairs(self, head):
        p=ListNode(0)
        p.next=head;head=p
        while p.next!=None and p.next.next!=None:
            s=p.next.next
            p.next.next=s.next
            s.next=p.next
            p.next=s
            p=s.next
        return head.next

时间: 2024-08-11 01:35:33

LeetCode 24 Swap Nodes in Pairs (C,C++,Java,Python)的相关文章

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

(Java) LeetCode 24. 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. Note: Your algorithm should use only constant extra space. You may not modify the values in the

LeetCode #24 Swap Nodes in Pairs (M)

[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

Java [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 li

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

[LeetCode]24. 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. Note: Your algorithm should use only constant extra space. You may not modify the values in the

【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] 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 题意: 把一个链表中的每一对节点对换