美文网首页
Reverse Linked List In Pairs

Reverse Linked List In Pairs

作者: GakkiLove | 来源:发表于2018-05-24 22:06 被阅读0次

    Reverse pairs of elements in a singly-linked list.

    Examples

    L = null, after reverse is null
    L = 1 -> null, after reverse is 1 -> null
    L = 1 -> 2 -> null, after reverse is 2 -> 1 -> null
    L = 1 -> 2 -> 3 -> null, after reverse is 2 -> 1 -> 3 -> null

    class Solution(object):
      def reverseInPairs(self, head):
        if not head or not head.next:
          return head
        next_node = head.next
        head.next = self.reverseInPairs(head.next.next)
        next_node.next = head
        return next_node
    

    相关文章

      网友评论

          本文标题:Reverse Linked List In Pairs

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