Shanshan Pythoner Love CPP

Leetcode (206, 92) Reverse Linked List I and II


LinkedList in Leetcode

Leetcode 206. Reverse Linked List

Leetcode 206. Reverse Linked List

Reverse a singly linked list.

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if (!head || !head->next)
            return head;
        ListNode* newHead = reverseList(head->next);
        head->next->next = head;
        head->next = NULL;
        return newHead;
    }
};

92. Reverse Linked List II]

92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:

Given 1->2->3->4->5->NULL, m = 2 and n = 4,

return 1->4->3->2->5->NULL.

class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        ListNode* prehead = new ListNode(0);
        prehead->next = head;
        ListNode* tail = prehead;
        n -= m;
        while(--m > 0) tail = tail->next;
        ListNode* curr = tail->next;
        while(n-- > 0){ // pull one out to tail
            ListNode* temp = curr->next;
            curr->next = temp->next; 
            temp->next = tail->next; // easy to make mistake
            tail->next = temp;
        }
        return prehead->next;
    }
};

Similar Posts

Comments

Content