Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

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]:

dummy = ListNode(None, None)


dummy.next = head
prev = dummy
cur = head
while cur and cur.next:

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:

i. Understanding and manipulating elements in an array.


ii. Implementing algorithms to manipulate linked lists, such as deleting nodes with
duplicate numbers.
iii. Solving problems that require a first-in-first-out (FIFO) or last-in-first-out (LIFO)
approach.
iv. Learning to optimize solutions to handle constraints and requirements effectively.

You might also like