反向链表

时间:2019-08-20 10:54:25

标签: python recursion linked-list singly-linked-list

下面给出了两个代码,用于反向链接列表,第一个工作正常,但是第二个工作不正常。有人可以解释为什么会这样吗?

工作代码:

def rev(curr, prev):
    if not curr:
        return prev
    next = curr.next
    curr.next = prev
    prev = curr
    curr = next
    return rev(curr, prev)

无效代码:

def rev(curr, prev):
    if not curr:
        return prev
    # next = curr.next
    curr.next = prev
    # prev = curr
    # curr = next
    return rev(curr.next,curr)
  

我以这种方式调用以上两个函数:

     

rev(头,无)

输入:

1->2->3->4->None

第一个代码的输出:

4->3->2->1->None

第二个代码的输出:

1->None

所需第二个代码的输出:

4->3->2->1->None

2 个答案:

答案 0 :(得分:1)

您在执行以下操作后将进行递归调用df['Date'] = pd.to_datetime(df.Date, format='%d.%m.%Y') s = df.groupby('CustomerId').One.transform('idxmin').sub(1) s1 = df.Date.sub(df.loc[s, ['Date']].reset_index(drop=True)['Date']).dt.days df['DateDiff'] = s1.where(df.Value.gt(0), 0) Out[363]: Date CustomerId Value One DateDiff 0 2017-01-02 02 0 1 0 1 2017-01-03 02 0 1 0 2 2017-01-04 02 10 0 1 3 2017-01-05 02 100 0 2 4 2017-01-01 03 0 1 0 5 2017-01-02 03 10000 0 1 6 2017-01-03 03 10000 0 2 7 2017-12-02 05 0 1 0 8 2017-12-03 05 0 0 0 9 2017-12-04 05 12312312 0 2

return rev(curr.next,curr)

因此,在您的初次通话curr.next = prev 中,您先设置了rev(head, None),然后用head.next = None调用了rev(并返回了头)。

更改为:

None

答案 1 :(得分:0)

在第二个代码中,您对将下一个ptr分配给当前ptr的行进行注释,并且不使用循环来迭代整个列表。那就是为什么它只显示列表的第一项。

您可以在下面的链接中找到链接列表的视频动画,这有助于您了解反向链接列表的概念。

参考链接:https://www.geeksforgeeks.org/reverse-a-linked-list/

代码:

class Node: 

    # Constructor to initialize the node object 
    def __init__(self, data): 
        self.data = data 
        self.next = None

class LinkedList: 

    # Function to initialize head 
    def __init__(self): 
        self.head = None

    # Function to reverse the linked list 
    def reverse(self): 
        prev = None
        current = self.head 
        while(current is not None): 
            next = current.next
            current.next = prev 
            prev = current 
            current = next
        self.head = prev 

    # Function to insert a new node at the beginning 
    def add(self, new_data): 
        new_node = Node(new_data) 
        new_node.next = self.head 
        self.head = new_node 

    # Utility function to print the linked LinkedList 
    def printList(self): 
        temp = self.head 
        while(temp): 
            print(temp.data) 
            temp = temp.next


# Driver program to test above functions 
llist = LinkedList() 
llist.add(20) 
llist.add(4) 
llist.add(15) 
llist.add(85) 

print("Given Linked List")
llist.printList() 
llist.reverse() 
print("\nReversed Linked List")
llist.printList()

输出:

Given Linked List                                                                                                       
85                                                                                                                      
15                                                                                                                      
4                                                                                                                       
20                                                                                                                      

Reversed Linked List                                                                                                    
20                                                                                                                      
4                                                                                                                       
15                                                                                                                      
85