728x90
LeetCode-24
swap-nodes-in-pairs : Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes
note : only nodes themselves may be changed.
MyAnswer
:::python
# 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]:
now = head
while now and now.next:
now.val, now.next.val = now.next.val, now.val
now = now.next.next
return head
Result : 36ms Memory: 13.8mb
๋ฐ์ํ
'๐ข One step' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[leetcode 92] Reverse Linked List II (0) | 2023.03.26 |
---|---|
[leetcode-24] Swap-Nodes-In-Pairs_2 (0) | 2023.03.21 |
[leetcode-2] Add Two Numbers (0) | 2023.03.17 |
[leetcode-206] Reverse Linked List (0) | 2023.03.15 |
[leetcode-21] Merge Two Sorted Lists (0) | 2023.03.12 |