Reverse Linked List In Pairs

2018-05-24  本文已影响0人  GakkiLove

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
上一篇下一篇

猜你喜欢

热点阅读