Reverse Linked List(dfs)

Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?

recursive记得return回新头(原来尾)

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """

        return self.helper(None,head)


    def helper(self,tail,cur):#tail是尾也是新头
        if not cur:
            return tail
        nxt = cur.next
        cur.next = tail

        return self.helper(cur,nxt)

iterative Null或者单个数不要倒了。记得返回的是prev

Last updated

Was this helpful?