0% found this document useful (0 votes)
20 views4 pages

Factacy - Ai Assignment

Uploaded by

chiku.myspot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Factacy - Ai Assignment

Uploaded by

chiku.myspot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Tushar Basu

Phone No.: 7488346191

Email: [email protected]

___________________________________________________________________________________

1. Given a string s, find the length of the longest substring without


repeating characters.

#code_in_python3

def longest_substring_length(s):

if not s:

return 0

max_length = 0

start = 0

char_index = {}

for i, char in enumerate(s):

if char in char_index and char_index[char] >= start:

start = char_index[char] + 1

else:

max_length = max(max_length, i - start + 1)

char_index[char] = i

return max_length

# Example

s = "iamtushar"

print("Length of the longest substring without repeating characters:", longest_substring_length(s))


2. Given the head of a linked list, remove the nth node from the end of the list and
return its head.

class ListNode:

def __init__(self, val=0, next=None):

self.val = val

self.next = next

def removeNthFromEnd(head, n):

dummy = ListNode(0)

dummy.next = head

first = dummy

second = dummy

for i in range(n + 1):

first = first.next

while first is not None:

first = first.next

second = second.next

second.next = second.next.next

return dummy.next

# Constructing a linked list


head = ListNode(1)

head.next = ListNode(2)

head.next.next = ListNode(3)

head.next.next.next = ListNode(4)

head.next.next.next.next = ListNode(5)

n = 2 # Remove the second node from the end

result_head = removeNthFromEnd(head, n)

while result_head is not None:

print(result_head.val, end=" -> ")

result_head = result_head.next

3. Given a linked list, swap every two adjacent nodes and return its
head. You must solve the problem without modifying the values in
the list's nodes.

class ListNode:

def __init__(self, val=0, next=None):

self.val = val

self.next = next

def swapPairs(head):

dummy = ListNode(0)

dummy.next = head

prev_node = dummy

while prev_node.next and prev_node.next.next:


first_node = prev_node.next

second_node = prev_node.next.next

# Swapping

prev_node.next = second_node

first_node.next = second_node.next

second_node.next = first_node

# Move to the next pair

prev_node = first_node

return dummy.next

# Example

head = ListNode(1)

head.next = ListNode(2)

head.next.next = ListNode(3)

head.next.next.next = ListNode(4)

head.next.next.next.next = ListNode(5)

result_head = swapPairs(head)

# Printing the modified linked list

while result_head is not None:

print(result_head.val, end=" -> ")

result_head = result_head.next

You might also like