Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

阿里第一题:另一种思路:如何实现一个高效的单向链表逆序输出? #81

Open
gethubwy opened this issue Jul 23, 2020 · 1 comment

Comments

@gethubwy
Copy link

如何实现一个高效的单向链表逆序输出?
顺序进栈,再依次弹出

@liuming-dev
Copy link

liuming-dev commented Jul 23, 2020

https://leetcode-cn.com/problems/reverse-linked-list/

func reverseList(head *ListNode) *ListNode {
    if head == nil {
        return head
    }

    previous := head
    current := head.Next
    head.Next = nil
    for current != nil {
        p := current.Next
        current.Next = previous
        previous = current
        current = p
    }

    return previous
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants