美文网首页
Day 23 两两交换链表

Day 23 两两交换链表

作者: 快乐的老周 | 来源:发表于2020-06-15 22:52 被阅读0次

    没看懂,只能先抄了再说

    class ListNode:
    def init(self, x):
    self.val = x
    self.next = None

    class Solution:
    def swapPairs(self, head):
    if head is None or head.next is None:
    return head
    tmp = head.next
    r = self.swapPairs(tmp.next)
    tmp.next = head
    head.next = r
    return tmp

    if name == 'main':
    a = [1,2,3,4,5]
    head = ListNode(a[0])

    tmp = head
    for i in a[1:]:
        node = ListNode(i)
        tmp.next = node
        tmp = tmp.next
    
    tmp = head
    while tmp:
        print(tmp.val)
        tmp = tmp.next
    
    shead = Solution().swapPairs(head)
    tmp = shead
    while tmp:
        print(tmp.val)
        tmp = tmp.next
    

    相关文章

      网友评论

          本文标题:Day 23 两两交换链表

          本文链接:https://www.haomeiwen.com/subject/aoskxktx.html