return head.next
    i = 0
    node = head
    prev = head
    while node is not None:
        node = node.next
        i += 1
        if i == size - n:
            prev.next = node.next
            return head
        prev = prev.next


if __name__ == "__main__":
    LL = SinglyLinkedList()
    LL.addAtHead(1)
    LL.addAtTail(2)
    LL.addAtTail(3)
    LL.addAtTail(4)
    LL.addAtTail(5)
    print("Before -> ", end="")
    print(LL)
    head = removeNthNodeFromEnd(LL.head, 2)
    print("After -> ", end="")
    printList(head)

    LL1 = SinglyLinkedList()
    LL1.addAtHead(1)
    print("Before -> ", end="")
    print(LL1)
    head = removeNthNodeFromEnd(LL1.head, 1)
  The number of the nodes in the list is in the range [0, 104].
  -105 <= Node.val <= 105
  pos is -1 or a valid index in the linked-list.

Follow up: Can you solve it using O(1) (i.e. constant) memory?
"""

from singlyLinkedList import SinglyLinkedList

def hasCycle(head):
  if head:
    fast = head.next
    slow = head
    while fast and fast.next:
      if fast.value == slow.value:
        return True 
      fast = fast.next.next
      slow = slow.next
  return False


if __name__ == "__main__":
  # Your LinkedList object will be instantiated and called as such:
  LL = SinglyLinkedList()
  LL.addAtHead(0)
  LL.addAtIndex(0, 2)
  LL.addAtIndex(0, 3)
  LL.addAtTail(-4)
  LL.addAtIndex(1, 30)
  print(LL)
  print(hasCycle(LL.head))
Exemplo n.º 3
0
    else: 
      #print(nodeA.val)
      nodeA = nodeA.next
    if nodeB is None: 
      nodeB = headA
    else: 
      #print(nodeB.val)
      nodeB = nodeB.next
  return nodeA.value


if __name__ == "__main__":
  intersect_value = 8
  intersect_in_LL_A = 2
  
  LL_A = SinglyLinkedList()
  LL_A.addAtHead(4)
  LL_A.addAtTail(1)
  LL_A.addAtTail(8)
  LL_A.addAtTail(4)
  LL_A.addAtTail(5)
  print(LL_A)

  LL_B = SinglyLinkedList()
  LL_B.addAtHead(5)
  LL_B.addAtTail(6)
  LL_B.addAtTail(1)
  LL_B.get(2).next = LL_A.get(intersect_in_LL_A)
  print(LL_B)

  print(getIntersectionNode(LL_A.head, LL_B.head))
Exemplo n.º 4
0
            odds.append(cur.value)
        pos += 1
        cur = cur.next
    LL = SinglyLinkedList()
    while evens:
        LL.addAtTail(evens[0])
        evens.pop(0)
    while odds:
        LL.addAtTail(odds[0])
        odds.pop(0)
    return LL.head


if __name__ == "__main__":
    LL = SinglyLinkedList()
    LL.addAtHead(1)
    LL.addAtTail(2)
    LL.addAtTail(3)
    LL.addAtTail(4)
    LL.addAtTail(5)
    head = getOddEvenList(LL.head)
    printList(head)

    LL_1 = SinglyLinkedList()
    LL_1.addAtHead(2)
    LL_1.addAtTail(1)
    LL_1.addAtTail(3)
    LL_1.addAtTail(5)
    LL_1.addAtTail(6)
    LL_1.addAtTail(4)
    LL_1.addAtTail(7)
    if head.value == value:
        head = head.next
        return head

    cur = head
    while cur and cur.next:
        if cur.next.value == value:
            cur.next = cur.next.next
        else:
            cur = cur.next
    return head


if __name__ == "__main__":
    LL = SinglyLinkedList()
    LL.addAtHead(4)
    LL.addAtTail(5)
    LL.addAtTail(1)
    LL.addAtTail(9)
    value = 5
    head = deleteNode(LL.head, value)
    printList(head)

    LL = SinglyLinkedList()
    LL.addAtHead(4)
    LL.addAtTail(5)
    LL.addAtTail(1)
    LL.addAtTail(9)
    value = 1
    head = deleteNode(LL.head, value)
    printList(head)
"""

from singlyLinkedList import SinglyLinkedList
from usefulLinkedListMethods import printList


def removeNodes(head, value):
    if not head:
        return head
    while head and head.value == value:
        head = head.next
    cur = head
    while cur and cur.next:
        if cur.next.value == value:
            cur.next = cur.next.next
        else:
            cur = cur.next
    return head


if __name__ == "__main__":
    LL = SinglyLinkedList()
    LL.addAtHead(1)
    LL.addAtTail(6)
    LL.addAtTail(3)
    LL.addAtTail(4)
    LL.addAtTail(5)
    LL.addAtTail(6)
    value = 6
    head = removeNodes(LL.head, value)
    printList(head)