본문 바로가기

반복 구조4

[LEET CODE] 92. Reverse Linked List II # Problem Given the head of a singly linked list and two integers left and right where left 2021. 3. 23.
[LEET CODE] 328. Odd Even Linked List # Problem Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. The first node is considered odd, and the second node is even, and so on. Note that the relative order inside both the even and odd groups should remain as it was in the input. Constraints: The number of nodes in the linked list i.. 2021. 3. 23.
[LEET CODE] 24. Swap Nodes in Pairs # Problem Given a linked list, swap every two adjacent nodes and return its head. Constraints: The number of nodes in the list is in the range [0, 100]. 0 ListNode: cur = head while cur and cur.next: # 값만 교환 cur.val, cur.next.val = cur.next.val, cur.val cur = cur.next.next return head # Solution 2 - 반복 구조로 스왑 # Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = .. 2021. 3. 23.
[LEET CODE] 206. Reverse Linked List # Problem Given the head of a singly linked list, reverse the list, and return the reversed list. Constraints: The number of nodes in the list is the range [0, 5000]. -5000 ListNode: def reverse(node: ListNode, prev: ListNode = None): if not node: return prev next, node.next = node.next, prev return reverse(next, node) return reverse(head) # Solution 2 - 반복 구조로 뒤집기 # Definition for singly-linked.. 2021. 3. 22.
반응형