Category : linked-list

I’ve tried this: class Node: def __init__(self, initdata): self.data = initdata self.next = None class LinkedList: def __init__(self): self.head = None self.tail = None def add(self, data): new_node = Node(data) if self.head == None: self.head = new_node if self.tail != None: self.tail.next = new_node self.tail = new_node def reverseList(self): if self.head == None or self.head.next ..

Read more

I have found a recursive solution to check whether a linked list is a palindrome: def isPalindrome(self, head: ListNode) -> bool: self.front_pointer = head def recursively_check(head): if head: if not recursively_check(head.next): return False if self.front_pointer.val != head.val: return False self.front_pointer = self.front_pointer.next return True return recursively_check() After the ‘if head’ statement: Why are we returning ..

Read more

I am reversing a singly- linked list with the following code: class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class Solution: def reverseList(self, head: ListNode) -> ListNode: if not head: return [] curr = head.next prev = head while curr: curr.next = prev curr = curr.next prev = prev.next return curr ..

Read more