题目描述
交换第k个节点和倒数第k个节点(即正数n-k-1, 如果从0数就n-k)
算法
y总比赛专用暴力。读出来列表操作然后输出成新链表(quote: itdef:解决90%链表问题)
def swapNodes(self, head: ListNode, k: int) -> ListNode:
nums = []
while head:
nums.append(head.val)
head = head.next
nums[k-1], nums[len(nums) - k] = nums[len(nums) - k], nums[k-1]
head1 = ListNode(nums[0])
result = head1
for i in range(1, len(nums)):
head1.next = ListNode(nums[i])
head1 = head1.next
return result