简体   繁体   中英

Python - reversing a linked list class module as input

I'm working on a problem that accepts a linked list as input, at this point I don't even know how to set up an example linked list.

My initial problem is understanding the following instruction:

Write a function that accepts a linked list as input, then reverses that linked list.

Does this simply involve defining a 'reversing summary' as part of the following or is there some other way of summarizing a linked lists in Python:

class Node(object):

    # Initialize with a single datum and pointer set to None

    def __init__(self, data=None, next_node=None):
        self.data = data
        self.next_node = next_node

    # Returns the stored data

    def get_data(self):
        return self.data

    # Returns the next node

    def get_next(self):
        return self.next_node

    # Reset the pointer to a new node

    def set_next(self, new_next):
        self.next_node = new_next

    def set_data(self, data):
        self.data = data

class LinkedList(object):

    # Top node in the list created on __init__

    def __init__(self, head=None):
        self.head = head

    # Take in the new node and point the new node to the prior head O(1)

    def insert(self, data):
        new_node = Node(data)
        new_node.set_next(self.head)
        self.head = new_node

    # Counts nodes O(n)

    def size(self):
        current = self.head
        count = 0
        while current:
            count += 1
            current = current.get_next()
        return count

    # Checks each node for requested data O(n)

    def search(self, data):
        current = self.head
        found = False
        while current and found is False:
            if current.get_data() == data:
                found = True
            else:
                current = current.get_next()
        if current is None:
            raise ValueError("Data not in list")
        return current

    # Similar to search, leapfrogs to delete, resetting previous node pointer to point to the next node in line O(n)

    def delete(self, data):
        current = self.head
        previous = None
        found = False
        while current and found is False:
            if current.get_data() == data:
                found = True
            else:
                previous = current
                current = current.get_next()
        if current is None:
            raise ValueError("Data not in list")
        if previous is None:
            self.head = current.get_next()
        else:
            previous.set_next(current.get_next())

It seems all of the code you have for your base Linked List class is there. You just need to reverse your Linked List. Reversing a linked list is like follows.

[1] -> [2] -> [3] -> NULL

You can tell that 1 is the Head and 3 is the Tail (meaning that it's the last node that is in the linked list because it points to NULL or None in Python). What you need to do is figure out some algorithm that will take that linked list and produce this result.

[3] -> [2] -> [1] -> NULL

Now 3 is the Head and 1 is the Tail.

So using Psuedocode:

Initalize 3 Nodes: prev, next, and current
current = head
prev = None
next = None
While current != None
    next = current.next
    current.next = prev
    prev = current
    current = next
head = prev

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM