Day 12 Task:
- Rotate Array: https://leetcode.com/problems/rotate-array/
- Intersection of Two Linked Lists: https://leetcode.com/problems/intersection-of-two-linked-lists/
Solutions:
Please review all the solutions and try implementing on your own if you haven’t gotten it.
1. Rotate Array
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k = k % len(nums)
nums.reverse()
nums[0:k] = reversed(nums[0:k])
nums[k::] = reversed(nums[k::])
2. Intersection of Two Linked Lists
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
lA = 0
lB = 0
temp = headA
while(temp):
lA = lA + 1
temp = temp.next
temp = headB
while(temp):
lB = lB + 1
temp = temp.next
if(lA > lB):
d = lA - lB
c = 1
temp1 = headA
while(c <= d):
temp1 = temp1.next
c = c + 1
temp2 = headB
else:
d = lB - lA
c = 1
temp1 = headB
while(c <= d):
temp1 = temp1.next
c = c + 1
temp2 = headA
flag = 0
while(temp1 and temp2):
if(temp1 == temp2):
flag = 1
return temp1
else:
temp1 = temp1.next
temp2 = temp2.next
return None