Skip to content

Commit

Permalink
Merge pull request #956 from sujijava/main
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Jul 1, 2024
2 parents 9899b70 + e4a4bb6 commit f885f1b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 207 deletions.
191 changes: 0 additions & 191 deletions asdfasdf.py

This file was deleted.

16 changes: 0 additions & 16 deletions sujioh/2024-06-23/best-time-to-buy-and-sell-stock.py

This file was deleted.

16 changes: 16 additions & 0 deletions sujioh/2024-06-30-reverse-a-linked-list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution(object):
def reverseList(self, head):
curr = head
prev = None

while curr:
next_curr = curr.next

# main execution
curr.next = prev

# prepare for next execution
prev = curr
curr = next_curr # 2 # 3

return prev
25 changes: 25 additions & 0 deletions sujioh/2024-06-30-rotate-list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if not head:
return None

tail = head
nodeLen = 1
while tail.next:
tail = tail.next
nodeLen += 1
tail.next = head # cycle 51234512345...
rotateType = k % nodeLen # 2

temp = head # 1234512345...
for _ in range(nodeLen - rotateType - 1): # 5-2-1 = 2
temp = temp.next # 3
res = temp.next # 4512345...
temp.next = None # 45123

return res
20 changes: 20 additions & 0 deletions sujioh/2024-06-30-swapping-nodes-in-a-linked-list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
l = head
for _ in range(k-1):
l = l.next
tail = l
r = head
while tail.next:
tail = tail.next
r = r.next
l.val, r.val = r.val, l.val
return head



0 comments on commit f885f1b

Please sign in to comment.