LeetCode 1721 Swapping Nodes in a Linked List (Python)

Posted by 小明MaxMing on March 14, 2021

题目

You are given the head of a linked list, and an integer k.

Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed).

解题思路

第一个指针先移动到第k个位置,之后第二个指针从头开始一起移动,第一个指针到末尾的时候,第二个指针在倒数第k个,交换两个指针的值

代码

class Solution:
    def swapNodes(self, head: ListNode, k: int) -> ListNode:
        first = head
        for i in range(k - 1):
            first = first.next
        tmp = first
        second = head
        while first.next:
            first = first.next
            second = second.next
        tmp.val, second.val = second.val, tmp.val
        return head

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅