当前位置: 首页>>代码示例>>Python>>正文


Python LinkedList.reverse方法代码示例

本文整理汇总了Python中linkedlist.LinkedList.reverse方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.reverse方法的具体用法?Python LinkedList.reverse怎么用?Python LinkedList.reverse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在linkedlist.LinkedList的用法示例。


在下文中一共展示了LinkedList.reverse方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_reverse_single

# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import reverse [as 别名]
 def test_reverse_single(self):
     ll = LinkedList(358)
     self.assertEqual(358, ll.peek())
     self.assertEqual(1, len(ll))
     ll.reverse()
     self.assertEqual(358, ll.peek())
     self.assertEqual(1, len(ll))
开发者ID:PeteRichardson,项目名称:practice-python,代码行数:9,代码来源:test_linkedlist.py

示例2: test_reverse

# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import reverse [as 别名]
 def test_reverse(self):
     ll = LinkedList(7)
     ll.append(8)
     ll.append(9)
     self.assertEqual(3, len(ll))
     self.assertEqual(7, ll.peek())
     self.assertEqual("7->8->9", str(ll))
     ll.reverse()
     self.assertEqual("9->8->7", str(ll))
     self.assertEqual(9, ll.peek())
开发者ID:PeteRichardson,项目名称:practice-python,代码行数:12,代码来源:test_linkedlist.py

示例3: test_reverse_double

# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import reverse [as 别名]
 def test_reverse_double(self):
     ll = LinkedList(45)
     ll.append(46)
     self.assertEqual(2, len(ll))
     self.assertEqual(45, ll.peek())
     self.assertEqual("45->46", str(ll))
     ll.reverse()
     self.assertEqual(2, len(ll))
     self.assertEqual(46, ll.peek())
     self.assertEqual("46->45", str(ll))
开发者ID:PeteRichardson,项目名称:practice-python,代码行数:12,代码来源:test_linkedlist.py

示例4: TestLinkedListReversal

# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import reverse [as 别名]
class TestLinkedListReversal(unittest.TestCase):

    def setUp(self):
        self.testelements = [5, 3, 6, 7, 8, 10, 6]
        self.expectedrev = [6, 10, 8, 7, 6, 3, 5]
        self.linkedlist = LinkedList()
        for e in self.testelements:
            self.linkedlist.append(e)

    def test_reversal(self):
        self.linkedlist.reverse()
        rev = [x for x in self.linkedlist]
        self.assertItemsEqual(self.expectedrev, rev)
开发者ID:samuelcstewart,项目名称:ads,代码行数:15,代码来源:test.py

示例5: test_reverse_empty

# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import reverse [as 别名]
 def test_reverse_empty(self):
     ll = LinkedList()
     self.assertTrue(ll.isEmpty())
     ll.reverse()
     self.assertTrue(ll.isEmpty())
开发者ID:PeteRichardson,项目名称:practice-python,代码行数:7,代码来源:test_linkedlist.py

示例6: reverse

# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import reverse [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()
开发者ID:goutkannan,项目名称:HackerRank,代码行数:33,代码来源:reverse_in_kgroup.py


注:本文中的linkedlist.LinkedList.reverse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。