0% found this document useful (0 votes)
4 views5 pages

Linked List LeetCode Problems

The document contains implementations of various linked list algorithms in Python, including reversing a list, removing elements, finding the middle node, deleting a node, converting a binary number to an integer, removing duplicates, merging two sorted lists, detecting cycles, checking for palindromes, finding intersections, inserting into a sorted circular list, and designing a linked list class. Each algorithm is encapsulated in a class named 'Solution' or 'MyLinkedList' with specific methods for the respective operations. These implementations utilize common linked list techniques and data structures.

Uploaded by

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

Linked List LeetCode Problems

The document contains implementations of various linked list algorithms in Python, including reversing a list, removing elements, finding the middle node, deleting a node, converting a binary number to an integer, removing duplicates, merging two sorted lists, detecting cycles, checking for palindromes, finding intersections, inserting into a sorted circular list, and designing a linked list class. Each algorithm is encapsulated in a class named 'Solution' or 'MyLinkedList' with specific methods for the respective operations. These implementations utilize common linked list techniques and data structures.

Uploaded by

Ayush Nair
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.206.

Reverse Linked List

class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = None
while head:
[Link], prev, head = prev, head, [Link]
return prev

2. 203. Remove Linked List Elements

class Solution:
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
dummy = ListNode(0)
[Link] = head
curr = dummy
while curr and [Link]:
if [Link] == val:
[Link] = [Link]
else:
curr = [Link]
return [Link]

3. 876. Middle of the Linked List

class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and [Link]:
slow = [Link]
fast = [Link]
return slow

4. 237. Delete Node in a Linked List

class Solution:
def deleteNode(self, node: Optional[ListNode]) -> None:
[Link] = [Link]
[Link] = [Link]

5. 1290. Convert Binary Number in a Linked List to Integer

class Solution:
def getDecimalValue(self, head: Optional[ListNode]) -> int:
num = 0
while head:
num = num * 2 + [Link]
head = [Link]
return num

6. 83. Remove Duplicates from Sorted List

class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
curr = head
while curr and [Link]:
if [Link] == [Link]:
[Link] = [Link]
else:
curr = [Link]
return head

7. 21. Merge Two Sorted Lists

class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Opt
dummy = ListNode()
curr = dummy
while list1 and list2:
if [Link] < [Link]:
[Link] = list1
list1 = [Link]
else:
[Link] = list2
list2 = [Link]
curr = [Link]
[Link] = list1 or list2
return [Link]

8. 141. Linked List Cycle

class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
slow = fast = head
while fast and [Link]:
slow = [Link]
fast = [Link]
if slow == fast:
return True
return False

9. 234. Palindrome Linked List

class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
vals = []
while head:
[Link]([Link])
head = [Link]
return vals == vals[::-1]

10. 160. Intersection of Two Linked Lists

class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode
a, b = headA, headB
while a != b:
a = [Link] if a else headB
b = [Link] if b else headA
return a

11. 708. Insert into a Sorted Circular Linked List


class Solution:
def insert(self, head: 'Optional[Node]', insertVal: int) -> 'Node':
newNode = Node(insertVal)
if not head:
[Link] = newNode
return newNode
curr = head
while True:
if [Link] <= insertVal <= [Link] or \
([Link] > [Link] and (insertVal >= [Link] or insertVal <= curr
[Link] == head:
[Link] = [Link]
[Link] = newNode
break
curr = [Link]
return head

12. 707. Design Linked List

class Node:
def __init__(self, val=0, next=None):
[Link] = val
[Link] = next

class MyLinkedList:

def __init__(self):
[Link] = None

def get(self, index: int) -> int:


curr = [Link]
for _ in range(index):
if not curr: return -1
curr = [Link]
return [Link] if curr else -1

def addAtHead(self, val: int) -> None:


[Link] = Node(val, [Link])

def addAtTail(self, val: int) -> None:


if not [Link]:
[Link] = Node(val)
return
curr = [Link]
while [Link]:
curr = [Link]
[Link] = Node(val)

def addAtIndex(self, index: int, val: int) -> None:


if index == 0:
[Link](val)
return
curr = [Link]
for _ in range(index - 1):
if not curr: return
curr = [Link]
if curr:
[Link] = Node(val, [Link])

def deleteAtIndex(self, index: int) -> None:


if index == 0:
if [Link]:
[Link] = [Link]
return
curr = [Link]
for _ in range(index - 1):
if not curr: return
curr = [Link]
if curr and [Link]:
[Link] = [Link]

You might also like