1 Star 0 Fork 0

iint/notes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
leetcode24.SwapNodesinPairs.md 2.92 KB
一键复制 编辑 原始数据 按行查看 历史
nuaazs 提交于 4年前 . first

## 24. Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.

Example 1:

<img alt="image-20210309165915103">

Input: head = [1,2,3,4]
Output: [2,1,4,3]

Example 2:

Input: head = []
Output: []

Example 3:

Input: head = [1]
Output: [1]

Constraints:

  • The number of nodes in the list is in the range [0, 100].
  • 0 <= Node.val <= 100

Solution

Pointer-pointer pp points to the pointer to the current node. So at first, pp points to head, and later it points to the next field of ListNodes. Additionally, for convenience and clarity, pointers a and b point to the current node and the next node.

cpp

We need to go from *pp == a -> b -> (b->next) to *pp == b -> a -> (b->next). The first three lines inside the loop do that, setting those three pointers (from right to left). The fourth line moves pp to the next pair.

ListNode* swapPairs(ListNode* head){
	ListNode **p = &head, *a, *b;
	while((a=*pp) && (b=a->next)){
		a->next = b->next;
		b->next = a;
		*pp = b;
		pp = &(a->next);
	}
	return head;
}
class Solution{
	public:
	ListNode* swapPairs(ListNode* head){
        if(head == NULL)
            return NULL;
        if(head->next == NULL)
            return head;
        ListNode* next = head->next;
        next->next = head;
        return next;
    }
};
class Solution{
    public:
    //invert the first 2 and then recurse for the rest
    ListNode* swapPairs(ListNode* head){
        //base case here
        if(!head || !head->next) return head;
        //Swapping part happens here, please draw to visualize
        ListNode *temp = head->next;
        head->next = swapPairs(temp->next);
        temp->next = head;
        return temp;
    }
};

it may be more clear to others by introducing a second tmp variable.

Comments are for the input list [1, 2, 3]

ListNode* swapPairs(ListNode* head){
    if(head == nullptr || head->next == nullptr) return head;
    // 2 is new head, 1 is head
    ListNode* new_head = head->next;
    // store 3
    ListNode* third = head->next->next;
    
    //2->1
    new_head->next = head;
    //1->3
    head->next = swapPairs(third);
    return new_head;
}

python

Here, pre is the previous node. Since the head doesn't have a previous node, I just use self instead. Again, a is the current node and b is the next node.

To go from pre -> a -> b -> b.next to pre -> b -> a -> b.next, we need to change those three references. Instead of thinking about in what order I change them, I just change all three at once.

def swapParis(self, head):
    pre, pre.next = self, head
    while pre.next and pre.next.next:
        a = pre.next
        b = a.next
        pre.next, b.next, a.next = b, a, b.next
        pre = a
    return self.next
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/iint/notes.git
git@gitee.com:iint/notes.git
iint
notes
notes
master

搜索帮助