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

[LeetCode] linked list #160

Open
plh97 opened this issue Aug 19, 2020 · 0 comments
Open

[LeetCode] linked list #160

plh97 opened this issue Aug 19, 2020 · 0 comments
Assignees
Labels
algorithm algorithm

Comments

@plh97
Copy link
Owner

plh97 commented Aug 19, 2020

反转链表

递归

var reverseList = function(head) {
    if (!head.next || !head ) return head  // if console.log will get 1,2,3,4,5
    const prev = reverseList(head.next)
    head.next.next = head   // if console.log here will get 5,4,3,2,1
    head.next = null
    return prev
};

双指针

var reverseList = function(head) {
    let prev = null
    let curr = head
    while(curr) {
        let temp = curr.next
        curr.next = prev
        prev = curr
        curr = temp
    }
    return prev
};
@plh97 plh97 added the algorithm algorithm label Aug 19, 2020
@plh97 plh97 self-assigned this Aug 19, 2020
@plh97 plh97 changed the title 链表 [LeetCode] link list Oct 6, 2020
@plh97 plh97 changed the title [LeetCode] link list [LeetCode] linked list Oct 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
algorithm algorithm
Projects
None yet
Development

No branches or pull requests

1 participant