LeetCode算法题python解法: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 list‘s nodes, only nodes itself may be changed.

中文翻译:

给定链表,交换每两个相邻节点并返回其头部。

例:

给定1->2->3->4,您应该将列表作为2->1->4->3

注意:

  • 您的算法应该只使用恒定的额外空间。
  • 您可能无法修改列表节点中的值,只能更改节点本身。

解题思路:该题非常简单,给定链表两两对调然后重新链接,返回新链表的头部即可。将链表遍历添加到一个列表linklist中,然后利用索引进行两两对调,最后重新连接这个链表,然后linklist[0]

代码如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
class Solution:
    def swapPairs(self, head):         #思路非常简单,把链表遍历出来放到列表里,然后再两两对调,在重新链接链表。
        linklist = []
        x, y = 0, 1

        while head != None:  #将链表遍历到一个linklist列表中
            linklist.append(head)
            head=head.next
        if len(linklist)<1:   #这里排除一下特殊情况,如果给的链表为空,则直接返回空值
            return linklist

        while y <= len(linklist)-1:      #这里进行两两对调
            linklist[x], linklist[y] = linklist[y], linklist[x]
            x, y = x + 2, y + 2

        for i in range(len(linklist)-1):        #将对调后的linklist链接成新的链表
            linklist[i].next=linklist[i+1]
        linklist[-1].next=None                   #设置链表最后一个值的末端为空值
        return linklist[0]

原文地址:https://www.cnblogs.com/slarker/p/9707661.html

时间: 2024-10-13 19:46:02

LeetCode算法题python解法:24. Swap Nodes in Pairs的相关文章

LeetCode算法题python解法:25. Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

LeetCode算法题python解法:11. Container With Most Water

LeetCode第十一题 Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis fo

LeetCode算法题python解法:23. Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input: [   1->4->5,   1->3->4,   2->6 ] Output: 1->1->2->3->4->4->5->6 题目大意就是给定多个已经排序的链表合并成一个排序的链表,我这里是将多个链表的值遍

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

24. Swap Nodes in Pairs(js)

24. Swap Nodes in Pairs 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-

[Leetcode][Python]24: Swap Nodes in Pairs

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 24: Swap Nodes in Pairshttps://oj.leetcode.com/problems/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 shoul

LeetCode算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)

这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个不同节点的值之间的最小差值.示例: 给定的树[4,2,6,1,3,null,null]由下图表示: 4 / 2 6 / \ 1 3 输出:1 说明:请注意,root是TreeNode对象,而不是数组.该树中的任意节点最小差值为1,它发生在节点1和节点2之间,也发生在节点3和节点2之间. 注意: BS

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

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