leetcode:206リバースリンクリスト

困難

簡単。

問題

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?

交流

class Solution():
    def reverseList(self, head):
        if not head:
            return head
        pre = dummy = ListNode(0)
        dummy.next = head
        cur = pre.next
        while cur.next:
            tmp = pre.next
            pre.next, cur.next, pre.next.next = cur.next, cur.next.next, tmp
        return dummy.next
公開された599元の記事 ウォンの賞賛856 ビュー184万+

おすすめ

転載: blog.csdn.net/JNingWei/article/details/83858121
おすすめ