[leetcode: Python]24. Swap Nodes in Pairs
来源:互联网 发布:中国宏观经济数据分析 编辑:程序博客网 时间:2024/05/19 22:24
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 list, only nodes itself can be changed.
Subscribe to see which companies asked this question.
方法一:49ms
交换的四个步骤如下图
这里加了个头结点。
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ if head is None or head.next is None: return head dummy = ListNode(0) dummy.next = head p = dummy while p.next and p.next.next: tmp = p.next.next p.next.next = tmp.next tmp.next = p.next p.next = tmp p = p.next.next return dummy.next
方法二:38ms
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ pre = self pre.next = head while pre.next and pre.next.next : a = pre.next b = a.next a.next = b.next pre.next = b b.next = a pre = a return self.next
方法三:35ms
递归
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ if not head or not head.next: return head cur = head head = cur.next cur.next = self.swapPairs(head.next) head.next = cur return head ''' if not head or not head.next: return head dummy = ListNode(0) dummy.next = head cur = dummy while cur.next and cur.next.next: # 如果不方便检查空,把ref挪到while里面让while来查也是很好的 n1 = cur.next n2 = cur.next.next n1.next = n2.next n2.next = n1 cur.next = n2 cur = n1 return dummy.next '''
阅读全文
0 0
- 24. Swap Nodes in Pairs Leetcode Python
- [leetcode: Python]24. Swap Nodes in Pairs
- 【LeetCode with Python】 Swap Nodes in Pairs
- Swap Nodes in Pairs Leetcode Python
- 【Leetcode】【python】Swap Nodes in Pairs
- 24. Swap Nodes in Pairs leetcode Python 2016 new Season
- python写算法题:leetcode: 24. Swap Nodes in Pairs
- LeetCode 24. Swap Nodes in Pairs (Python)
- [LeetCode]24.Swap Nodes in Pairs
- LeetCode --- 24. Swap Nodes in Pairs
- LeetCode 24.Swap Nodes in Pairs
- [Leetcode] 24. Swap Nodes in Pairs
- 【LeetCode】24.Swap Nodes in Pairs
- [leetcode] 24.Swap Nodes in Pairs
- **LeetCode 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs LeetCode
- leetcode 24. Swap Nodes in Pairs
- leetcode 24. Swap Nodes in Pairs
- Shell script to build fdk-aac for use in iOS apps.
- 树的中序遍历
- form表单的各种属性简单列举
- 监听按钮的点击事件
- 数据挖掘数据的资料分享
- [leetcode: Python]24. Swap Nodes in Pairs
- 顶部导航条
- cocosstudio3.0以上版本控件高级属性之用户数据的使用
- Android studio打包签名报错:Error:(3) Error: "app_name" is not translated in "am" (Amharic);
- jmeter录制移动app脚本
- java中子类的调用
- MFC子窗口向父窗口发送消息(测试成功20110117)
- 1097. Deduplication on a Linked List (25)
- 超级课程表课表的界面的实现