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) print("After -> ", end="") printList(head) LL2 = SinglyLinkedList() LL2.addAtHead(1) LL2.addAtTail(2) print("Before -> ", end="") print(LL2) head = removeNthNodeFromEnd(LL2.head, 1)
def isPalindromeLinkedList(head): nodes = [] cur = head while cur: nodes.append(str(cur.value)) cur = cur.next #nodes_str = "".join(nodes) #print(nodes_str) #nodes.reverse() #nodes_str_rev = "".join(nodes) #print(nodes_str_rev) if nodes == nodes[::-1]: return True return False if __name__ == "__main__": LL = SinglyLinkedList() LL.addAtHead(1) LL.addAtTail(2) printList(LL.head) print(isPalindromeLinkedList(LL.head)) LL_1 = SinglyLinkedList() LL_1.addAtHead(1) LL_1.addAtTail(2) LL_1.addAtTail(2) LL_1.addAtTail(1) printList(LL_1.head) print(isPalindromeLinkedList(LL_1.head))