Sunday, February 26, 2023
HomeSoftware EngineeringLearn how to Reverse a singly-linked listing in Python

Learn how to Reverse a singly-linked listing in Python

The problem

Implement a perform reverse_list that takes a singly-linked listing of nodes and returns an identical listing within the reverse order.

Assume the presence of a category Node, which exposes the property worth/Worth and subsequent/Subsequentsubsequent should both be set to the subsequent Node within the listing, or to None (or null) to point the tip of the listing.

To help in writing checks, a perform make_linked_list (Node.asLinkedList() in Java) has additionally been outlined, which converts a python listing to a linked listing of Node.

The ultimate checks will use a really lengthy listing. Bear in mind {that a} recursive resolution will run out of stack.

The answer in Python code

Choice 1:

def reverse_list(node):
    res = None
    whereas node:
        res = Node(node.worth, res)
        node = node.subsequent
    return res

Choice 2:

def reverse_list(head):
    tail = None
    whereas head:
        tail, head.subsequent, head = head, tail, head.subsequent
    return tail

Choice 3:

def reverse_list(node):
    earlier = None
    whereas node:
        earlier, node, earlier.subsequent = node, node.subsequent, earlier
    return earlier

Take a look at instances to validate our resolution

# create linked lists for testing by chaining nodes
take a look at.assert_equals(reverse_list(Node(1, Node(2, Node(3, None)))), Node(3, Node(2, Node(1, None))))
# or alternately use the helper perform
take a look at.assert_equals(reverse_list(make_linked_list([1, 2, 3, 4, 5])), make_linked_list([5, 4, 3, 2, 1]))


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

situs slot gacor provider terbaik agen toto slot terpercaya 2023 agen toto togel terpercaya 2023 situs toto togel pasaran resmi terbaik bandar toto macau pasaran resmi toto togel bandar toto slot gacor 4d 2023 bo togel online pasaran terlengkap sepanjang masa bo toto slot terlengkap sepanjang masa situs toto togel 2023 bet 100 perak daftar toto slot dan toto togel 2023 bermain toto togel dengan bet hanya 100 perak daftar toto slot bonus new member terpercaya bermain toto slot pelayanan 24 jam nonstop agen slot gacor 4d hadiah terbesar bandar toto slot provider terbaik toto slot gacor 4d hingga toto togel toto togel pasaran resmi terpercaya bo togel online terbaik 2023 agen togel online terbesar 2023 situs togel online terpercaya 2023 bo togel online paling resmi 2023 toto togel pasaran togel hongkong resmi situs slot online pasti gacor agen slot online anti rungkad bo slot online deposit tanpa potongan situs toto togel dan toto slot bonus new member situs toto slot gacor 4d bo toto slot gacor 4d bo toto slot gacor dari toto togel 4d bo toto slot 4d terpercaya bo toto slot terpercaya toto macau resmi dari toto togel 4d agen togel terbesar dan situs toto slot terpercaya bandar toto togel dan slot online 2023 bo slot gacor terbaik sepanjang masa winsortoto winsortoto bo toto togel situs toto situs toto togel terpercaya situs toto slot terpercaya situs slot gacor 4d terbaik sepanjang masa agen toto togel dan situs toto slot terpercaya situs toto togel dan agen toto slot terpercaya bandar toto togel tersedia pasaran toto macau resmi agen toto togel bet 100 perak deposit 10rb ltdtoto