Linked list Medium problems
19. Remove Nth Node From End of List
Problem
https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
Given the head of a linked list, remove the nth node from the end of the list and return its head.
Example

- Input: head = [1,2,3,4,5], n = 2
- Output: [1,2,3,5]
Solution
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy = ListNode(0, head)
first = second = dummy
# Move first pointer n+1 steps ahead
for _ in range(n + 1):
first = first.next
# Move both pointers until first reaches the end
while first:
first = first.next
second = second.next
# Remove the nth node from the end
second.next = second.next.next
return dummy.next

Explanation
The removeNthFromEnd function is designed to remove the nth node from the end of a singly linked list. Here's a step-by-step explanation of how it works:
- Initialization:
- A dummy node is created and linked to the head of the list. This helps handle edge cases, such as when the list has only one node or when the head itself needs to be removed.
-
Two pointers,
firstandsecond, are initialized to point to the dummy node. -
Advance the
firstPointer: -
The
firstpointer is movedn + 1steps forward. This ensures that the gap between thefirstandsecondpointers isnnodes apart. -
Move Both Pointers:
-
Both
firstandsecondpointers are moved one step at a time until thefirstpointer reaches the end of the list. At this point, thesecondpointer will be pointing to the node just before the one that needs to be removed. -
Remove the Nth Node:
-
The
secondpointer'snextis adjusted to skip the nth node from the end, effectively removing it from the list. -
Return the New Head:
- The function returns
dummy.next, which is the head of the modified list.
24. Swap Nodes in Pairs
https://leetcode.com/problems/swap-nodes-in-pairs//
Problem Description
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 (i.e., only nodes themselves may be changed.)
-
Input: head = [1,2,3,4]
-
Output: [2,1,4,3]
-
Explanation:

Solution
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0)
dummy.next = head
prev, current = dummy, head
while current and current.next:
# Nodes to be swapped
first = current
second = current.next
# Swapping
prev.next = second
first.next = second.next
second.next = first
# Re-position prev and current
prev = first
current = first.next
return dummy.next

Explanation
- Initialization:
- A dummy node is created and linked to the head of the list. This simplifies the process of swapping the head node.
-
Two pointers,
prevandcurrent, are initialized to point to the dummy node and the head of the list, respectively. -
Iterate through the List:
-
The loop continues as long as there are at least two nodes to swap (
currentandcurrent.next). -
Swapping Nodes:
- Identify the two nodes to be swapped:
first(current node) andsecond(next node). -
Adjust the pointers to swap the nodes:
prev.nextis set tosecond.first.nextis set tosecond.next.second.nextis set tofirst.
-
Move to the Next Pair:
-
Update
prevtofirstandcurrenttofirst.nextto continue with the next pair of nodes. -
Return the New Head:
- The function returns
dummy.next, which is the head of the modified list.