本文整理汇总了Python中linkedlist.LinkedList.head方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.head方法的具体用法?Python LinkedList.head怎么用?Python LinkedList.head使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类linkedlist.LinkedList
的用法示例。
在下文中一共展示了LinkedList.head方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: reverse_in_place
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import head [as 别名]
def reverse_in_place(ll):
"""
Steps:
a -> b -> c
<- a b -> c * When we are changing b we need references to a and c
<- a <- b c
<- a <- b <- c
"""
if ll.length < 2:
return ll
previous = ll.head
current = previous.next
later = current.next
previous.next = None
while True:
current.next = previous
previous = current
current = later
if current is not None:
later = current.next
else:
break
reverse_list = LinkedList()
reverse_list.head = previous
return reverse_list
示例2: addition
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import head [as 别名]
def addition(llist1,llist2):
curr1 = llist1.head
curr2 = llist2.head
myList = LinkedList()
carry = 0
temp = None
while(curr1 is not None or curr2 is not None):
lvalue= curr1.data if curr1 else 0
rvalue= curr2.data if curr2 else 0
val = lvalue + rvalue + carry
carry = 1 if val >10 else 0
val = val % 10
newNode = Node(val)
if myList.head is None:
myList.head = newNode
temp = myList.head
else:
temp.next = newNode
temp = temp.next
if curr1 is not None:
curr1 = curr1.next
if curr2 is not None:
curr2 = curr2.next
if carry>0:
temp.next = Node(carry)
return myList
示例3: isPalindrome
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import head [as 别名]
return True
f = isPalindrome(right.next)
if f is False:
return False
global left
flag= (left.data == right.data)
left = left.next
return flag
llist1 = LinkedList()
llist1.head = Node(2)
llist1.insert(4)
llist1.insert(7)
llist1.insert(7)
llist1.insert(4)
llist1.insert(2)
llist1.printList()
left = llist1.head
print(isPalindrome(llist1.head) )
示例4: while
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import head [as 别名]
myList.insertFront(curr1.data)
curr1 = curr1.next
#Left overs in either list
while(curr1):
myList.insertFront(curr1.data)
curr1 = curr1.next
while(curr2):
myList.insertFront(curr2.data)
curr2 = curr2.next
return myList
llist1 = LinkedList()
llist1.head = Node(2)
llist1.insert(4)
llist1.insert(7)
llist1.insert(8)
llist1.insert(12)
llist1.insert(20)
llist2 = LinkedList()
llist2.head = Node(1)
llist2.insert(14)
llist2.insert(27)
llist2.insert(38)
print("Befor Merge Sort")
llist1.printList()
示例5: reverse
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import head [as 别名]
# cool implementation of reverse with
def reverse(self, head, k):
current = head
nextNode = None
prev = None
count =0
while current is not None and count<k:
nextNode = current.next
current.next = prev
prev = current
current = nextNode
count+=1
if nextNode is not None:
head.next = self.reverse(nextNode,k)
return prev
llist1 = LinkedList()
llist1.head = Node(2)
llist1.insert(4)
llist1.insert(7)
llist1.insert(8)
llist1.insert(12)
llist1.insert(20)
llist1.printList()
llist1.head = llist1.reverse(llist1.head,3)
llist1.printList()
示例6: TestLinkedList
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import head [as 别名]
class TestLinkedList(unittest.TestCase):
def setUp(self):
"""Test setup"""
self.empty_list = LinkedList()
self.A_LIST_HEAD = 42
self.a_list = LinkedList(self.A_LIST_HEAD)
def test_new_list(self):
"""A criação de uma Nova Lista"""
self.assertIsInstance(self.empty_list, LinkedList)
def test_new_list_empty(self):
"""Uma Nova Lista criada sem parâmetros é vazia"""
self.assertTrue(self.empty_list.is_empty())
def test_empty_list_equality(self):
"""Listas Vazias são iguais"""
a = LinkedList()
b = LinkedList()
self.assertEqual(self.empty_list, a)
self.assertEqual(self.empty_list, b)
self.assertEqual(a, b)
def test_new_list_not_empty_with_value(self):
"""Uma Nova Lista criada com parâmetros é não-vazia"""
self.assertFalse(self.a_list.is_empty())
def test_empty_list_size(self):
"""Uma Lista vazia possui tamanho zero"""
self.assertEqual(self.empty_list.size(), 0)
def test_a_list_with_one_element_size_is_one(self):
"""Uma Lista com um elemento possui tamanho 1"""
self.assertEqual(self.a_list.size(), 1)
def test_a_list_head_is_the_first_element(self):
"""A cabeça de uma lista é seu primeiro elemento"""
"""Caso a lista seja vazia, o valor da cabeça é None"""
self.assertEqual(self.empty_list.head().value,None)
self.assertEqual(self.a_list.head().value, self.A_LIST_HEAD)
def test_a_list_tail_is_the_rest_of_the_list(self):
"""A cauda de uma lista são todos os elementos menos a cabeça"""
"""Caso a lista seja vazia ou tenha somente um elemento,
a Cauda é uma Lista Vazia"""
self.assertEqual(self.empty_list.tail(),LinkedList())
self.assertEqual(self.a_list.tail(), LinkedList() )
def test_append_changes_list_size(self):
"""Append insere um elemento no final da lista"""
"""A operação append aumenta o tamanho da lista em 1"""
size = self.a_list.size()
self.a_list.append(55)
new_size = size + 1
self.assertEqual(self.a_list.size(), new_size)
def test_append_changes_list_size_multiple_elements(self):
"""X appends aumentam o tamanho da lista em X elementos"""
size = self.a_list.size()
x = random.randint(1, 200)
for i in range(0, x):
value = random.randint(1, 10000)
self.a_list.append(value)
new_size = size + x
self.assertEqual(self.a_list.size(), new_size)
def test_append_empty_list_appended_element_is_head(self):
"""Append numa lista vazia torna o novo elemento a cabeça da lista"""
self.assertTrue(self.empty_list.is_empty())
self.assertTrue(self.empty_list.head().is_empty())
x = 42
self.empty_list.append(x)
self.assertEqual(self.empty_list.head().value, x)
def test_prepend_changes_list_size(self):
"""Prepend insere um elemento no início da lista"""
"""A operação prepend aumenta o tamanho da lista em 1"""
size = self.a_list.size()
self.a_list.prepend(98)
new_size = size + 1
self.assertEqual(self.a_list.size(), new_size)
def test_prepend_changes_list_size_multiple_elements(self):
"""X prepends aumentam o tamanho da lista em X elementos"""
size = self.a_list.size()
x = random.randint(1, 200)
#.........这里部分代码省略.........