leetcode 24. Swap Nodes in Pairs ( 2 个一组翻转链表)链表

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->4->3.

思路:链表操作

/**
 * 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=&head;ListNode* a,* b;
        while((a=*p)&&(b=a->next)){
            a->next=b->next;
            b->next=a;
            *p=b;
            p=&(a->next);
        }
        return head;
    }
};

迭代

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummy=new ListNode(-1);
        dummy.next=head;
        ListNode pre=dummy;
        ListNode cur,nxt;
        while((cur=pre.next)!=null&&(nxt=cur.next)!=null){
            cur.next=nxt.next;
            nxt.next=pre.next;
            pre.next=nxt;
            pre=cur;
        }
        return dummy.next;
    }
}

C++

  • 这样写更好理解一点
/**
 * 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* dummy=new ListNode(-1);
        dummy->next=head;
        ListNode* pre=dummy;
        ListNode* cur,* nxt;
        while((cur=pre->next)&&(nxt=cur->next)){
            cur->next=nxt->next;
            nxt->next=cur;
            pre->next=nxt;
            pre=cur;
        }
        return dummy->next;
    }
};

Python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(self, head: ListNode) -> ListNode:
        dummy=ListNode(-1)
        dummy.next=head
        pre=dummy
        while pre.next and pre.next.next:
            cur,nxt=pre.next,pre.next.next
            cur.next=nxt.next
            nxt.next=pre.next
            pre.next=nxt
            pre=cur
        return dummy.next

递归

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head==null||head.next==null)return head;
        ListNode p=head.next;
        head.next=swapPairs(head.next.next);
        p.next=head;
        return p;
    }
}

C++

  • 这个递归还是很妙的
/**
 * 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->next;
        head->next=swapPairs(head->next->next);
        p->next=head;
        return p;
    }
};

Python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(self, head: ListNode) -> ListNode:
        if head is None or head.next is None:
            return head
        p=head.next
        head.next=self.swapPairs(head.next.next)
        p.next=head
        return p
posted @ 2020-07-23 07:52  winechord  阅读(88)  评论(0编辑  收藏  举报