zoukankan      html  css  js  c++  java
  • [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-
    '''
    __author__ = 'dabay.wang@gmail.com'

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

    ===Comments by Dabay===
    链表的操作。主要就是赋值next的时候,如果这个next本来指向的node还有用,就把它先记录下来。
    这里用previous指向要交换的一对node的前面一个node。要交换的node依次为node1和node2。
    '''

    # Definition for singly-linked list.
    class ListNode:
    def __init__(self, x):
    self.val = x
    self.next = None


    class Solution:
    # @param a ListNode
    # @return a ListNode
    def swapPairs(self, head):
    previous = new_head = ListNode(0)
    new_head.next = head
    while previous and previous.next and previous.next.next:
    node1 = previous.next
    node2 = node1.next
    node1.next = node2.next
    previous.next = node2
    node2.next = node1
    previous = node1
    return new_head.next


    def print_listnode(node):
    while node:
    print "%s->" % node.val,
    node = node.next
    print "END"


    def main():
    sol = Solution()
    node = root = ListNode(1)
    for i in xrange(2, 5):
    node.next = ListNode(i)
    node = node.next
    print_listnode(root)
    print_listnode(sol.swapPairs(root))


    if __name__ == '__main__':
    import time
    start = time.clock()
    main()
    print "%s sec" % (time.clock() - start)
  • 相关阅读:
    怎么获取当前页面的URL
    asp.net设置元素css的属性
    跨页面传值
    html怎么添加背景图片
    web.config配置数据库连接
    SQL Server游标的使用【转】
    动态生成表格呈现还是将表格直接绑定gridview等控件呈现的开发方式选择依据
    T-SQL查询进阶--变量
    sql server 中引號嵌套
    SQLServer中临时表与表变量的区别分析
  • 原文地址:https://www.cnblogs.com/Dabay/p/4257570.html
Copyright © 2011-2022 走看看