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

Link List

Uploaded by

krishkdd0
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)
11 views4 pages

Link List

Uploaded by

krishkdd0
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

class Solution:

def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:


if not head:
return None

newHead = head
if head.next:
newHead = self.reverseList(head.next)
head.next.next = head
head.next = None

return newHead

class ListNode:
def __init__(self,value):
self.value = value
self.next = None

class Solution:
def deleteNode(self, node:ListNode):
node.val = node.next.val
node.next = node.next.next
return

class ListNode:
def __init__(self,value):
self.value = value
self.next = None

class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
slow, fast = head, head

while fast and fast.next:


slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False

class Solution:
def mergeTwoLists(self, list1: ListNode, list2: ListNode) -> ListNode:
dummy = node = ListNode()

while list1 and list2:


if list1.val < list2.val:
node.next = list1
list1 = list1.next
else:
node.next = list2
list2 = list2.next
node = node.next

node.next = list1 or list2

return dummy.next

class Solution(object):
def middleNode(self, head):
slow_pointer = head
fast_pointer = head

while fast_pointer is not None and fast_pointer.next is not None:


slow_pointer = slow_pointer.next
fast_pointer = fast_pointer.next.next

return slow_pointer
class ListNode:
def __init__(self,value):
self.value = value
self.next = None

class Solution:
def isPalindrome (self, head: ListNode) -> bool:
fast = head
slow = head
# find middle (slow)

while fast and fast.next:


fast = fast.next.next
slow=slow.next

# reverse second half


prev = None
while slow:
tmp = slow.next
slow.next = prev
prev = slow
slow=tmp

# check palindrome
left, right=head, prev
while right:
if left.val != right.val:
return False
left= left.next
right = right.next
return True

class ListNode:
def __init__(self,value):
self.value = value
self.next = None
class Solution:
def removeElements (self, head: ListNode, val: int) -> ListNode:
dummy= ListNode (next-head)
prev, curr = dummy, head
while curr:
nxt = curr.nxt
if curr.val == val:
prev.next = nxt
else:
prev = curr
curr = nxt
return dummy.next

You might also like