Professional Documents
Culture Documents
AP 1.1 4422(1)
AP 1.1 4422(1)
Experiment 1.1
Student Name: Manohar Chaudhary UID: 21BCS4422
Branch: BE- CSE Section/Group: FL-603-B
Semester: 6th Date of Performance:16/1/23
Subject Name: AP-2 Subject Code:21CSH-351
1. Aim: To implement the concept of Arrays, Queues and Stack and Linked List.
1. Problem statement - Given an integer array nums, return all the triplets [nums[i],
nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] +
nums[k] == 0.Notice that the solution set must not contain duplicate triplets.
2. Problem statement - Given the head of a sorted linked list, delete all nodes that
have duplicate numbers, leaving only distinct numbers from the original list.
Return the linked list sorted as well.
2. Objective:
• Master data structure manipulation: Arrays, Queues, Stacks, Linked Lists.
• Apply data structures to solve specific challenges: finding triplets in arrays and
removing duplicates in sorted linked lists.
3. Code
1.
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
target = 0
nums.sort()
s = set()
output = []
for i in range(len(nums)):
j = i + 1
k = len(nums) - 1
while j < k:
sum = nums[i] + nums[j] + nums[k]
if sum == target:
s.add((nums[i], nums[j], nums[k]))
j += 1
k -= 1
elif sum < target:
j += 1
else:
k -= 1
output = list(s)
return output
2.
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) ->
Optional[ListNode]:
if cur.next.val == cur.val:
while cur.next and cur.next.val == cur.val:
cur = cur.next
cur.next, cur = None, cur.next
prev.next = cur
else:
prev = cur
cur = cur.next
return dummy.next
4. OUTPUT
1.
2.
5. Learning Outcomes: