mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 14:57:08 +00:00
Time: 35 ms (67.17%), Space: 13.9 MB (60.50%) - LeetHub
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
# Definition for singly-linked list.
|
||||
# class ListNode:
|
||||
# def __init__(self, val=0, next=None):
|
||||
# self.val = val
|
||||
# self.next = next
|
||||
class Solution:
|
||||
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
|
||||
if head == None or head.next == None:
|
||||
return head
|
||||
|
||||
first = head
|
||||
second = head.next
|
||||
|
||||
first.next = second.next
|
||||
second.next = first
|
||||
|
||||
first.next = self.swapPairs(first.next)
|
||||
|
||||
return second
|
||||
Reference in New Issue
Block a user