Linked List-2 Ass Sol
Linked List-2 Ass Sol
Assignment Solution
You are given the head of a linked list. Delete the middle node, and return the head of the modified linked
list. [Leetcode 2095]
The middle node of a linked list of size n is the ⌊n / 2⌋th node from the start using 0-
based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x .
Example 1:
Output: [1,3,4,1,2,6]
Explanation:
The above figure represents the given linked list. The indices of the nodes are written below.
Since n = 7, node 3 with value 7 is the middle node, which is marked in red.
Example 2:
Output: [1,2,4]
Explanation:
For n = 4, node 2 with value 3 is the middle node, which is marked in red.
Example 3:
Output: [2]
Explanation:
For n = 2, node 1 with value 1 is the middle node, which is marked in red.
Node 0 with value 2 is the only node remaining after removing node 1.
class ListNode:
self.val = val
self.next = next
class Solution:
return None
slow = head
fast = head
prev = None
prev = slow
slow = slow.next
fast = fast.next.next
prev.next = slow.next
return head
def create_linked_list(arr):
head = ListNode(arr[0])
current = head
current.next = ListNode(val)
current = current.next
return head
def display_linked_list(head):
current = head
while current:
current = current.next
print("None")
# Example usage
sol = Solution()
new_head = sol.deleteMiddle(head)
display_linked_list(new_head)
Q2.You are given two linked lists: list1 and list2 of sizes n and m respectively.
Remove list1 's nodes from the ath node to the bth node, and put list2 in their place.
[Leetcode 1669]
The blue edges and nodes in the following figure indicate the result:
Example 1
Output: [0,1,2,1000000,1000001,1000002,5]
Explanation: We remove the nodes 3 and 4 and put the entire list2 in their place. The blue
Example 2:
[1000000,1000001,1000002,1000003,1000004]
Output: [0,1,1000000,1000001,1000002,1000003,1000004,6]
Explanation: The blue edges and nodes in the above figure indicate the result.
class ListNode:
self.val = val
self.next = next
class Solution:
current = list1
current = current.next
node_before_a = current
current = current.next
node_after_b = current
node_before_a.next = list2
while list2.next:
list2 = list2.next
list2.next = node_after_b
return list1
# Example usage
def create_linked_list(arr):
head = ListNode(arr[0])
current = head
current.next = ListNode(val)
current = current.next
return head
def display_linked_list(head):
current = head
while current:
current = current.next
print("None")
a, b = 3, 4
sol = Solution()
display_linked_list(new_head)
Return the head of the linked list after swapping the values of the kth node from the
beginning and the kth node from the end (the list is 1-indexed). [Leetcode 1721]
Example 1:
Output: [1,4,3,2,5]
Example 2:
Output: [7,9,6,6,8,7,3,0,9,5]
class ListNode:
self.val = val
self.next = next
class Solution:
size = 0
current = head
while current:
size += 1
current = current.next
return size
size = self.getSize(head)
start_node = head
end_node = head
start_node = start_node.next
end_node = end_node.next
start_node.val
return head
# Example usage
def create_linked_list(arr):
head = ListNode(arr[0])
current = head
current.next = ListNode(val)
current = current.next
return head
def display_linked_list(head):
current = head
while current:
current = current.next
print("None")
k = 2
sol = Solution()
new_head = sol.swapNodes(head, k)
display_linked_list(new_head)
Given the head of a linked list and an integer val , remove all the nodes of the linked list that has Node.val
== val , and return the new head.
Example 1:
Output: [1,2,3,4,5]
Example 2:
Output: []
Example 3:
Output: []
class ListNode:
self.val = val
self.next = next
class Solution:
dummy = ListNode(next=head)
current = dummy
while current.next:
if current.next.val == val:
current.next = current.next.next
else:
current = current.next
return dummy.next
# Example usage
def create_linked_list(arr):
if not arr:
return None
head = ListNode(arr[0])
current = head
current.next = ListNode(val)
current = current.next
return head
def display_linked_list(head):
current = head
while current:
current = current.next
print("None")
# Example 1
sol = Solution()
new_head1 = sol.removeElements(head1, 6)
display_linked_list(new_head1)
# Example 2
head2 = create_linked_list([])
new_head2 = sol.removeElements(head2, 1)
display_linked_list(new_head2)
# Example 3
new_head3 = sol.removeElements(head3, 7)
display_linked_list(new_head3)
class ListNode:
self.val = val
self.next = next
class Solution:
slow = head
fast = head
slow = slow.next
fast = fast.next.next
if slow == fast:
return self.countNodesInLoop(slow)
return 0
count = 1
current = meetingPoint.next
count += 1
current = current.next
return count
tail = head
while tail.next:
tail = tail.next
loop_start = head
for _ in range(position):
loop_start = loop_start.next
tail.next = loop_start
def create_linked_list(arr):
head = ListNode(arr[0])
current = head
current.next = ListNode(val)
current = current.next
return head
def display_linked_list(head):
current = head
if not current:
break
current = current.next
print("...")
# Example usage
sol = Solution()
sol.createLoop(head, 2)