LeetCode链表问题

67 阅读2分钟

1. 剑指 Offer II 024. 反转链表

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* pre = nullptr;
        ListNode* cur = head;
        ListNode* nex = nullptr;

        while (cur)
        {
            nex = cur -> next;
            cur -> next = pre;
            pre = cur;
            cur = nex;
        }

        return pre;
    }
};

2. LeetCode 24. 两两交换链表中的节点

题解:递归

class Solution {
public:
	ListNode *swapPairs(ListNode *head) {
		if(head==nullptr||head->next==nullptr)return head;
		//递归法

		//head为旧链表的头结点,新链表的第二个结点
		//newHead = head->next为旧链表的第二个结点,新链表的第一个结点
		ListNode *newHead = head->next;//新的头结点为原来头结点的下一个结点
		head->next = swapPairs(newHead->next);//原来头结点的下一个结点为下一组交换后的新的头结点
		newHead->next = head;

		return newHead;
	}
};

3. LeetCode 141. 环形链表

题解一:哈希表

class Solution {
public:
    bool hasCycle(ListNode *head) {
        unordered_set<ListNode*> seen;
        while (head != nullptr)
        {
            if (seen.count(head))
            {
                return true;
            }
            seen.insert(head);
            head = head->next;
        }
        return false;
    }
};

题解二:快慢指针

class Solution {
public:
    bool hasCycle(ListNode *head) {
        if (!head) return false;
        ListNode* s = head;
        ListNode* f = head->next;
        while (s && f)
        {
            s = s->next;
            f = f->next;
            if (!f) break;
            f = f->next;
            if (s == f) return true;
        }
        return false;
    }
};

4. LeetCode 142. 环形链表 II

解法一:哈希表

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        unordered_set<ListNode*> seen;
        while (head != nullptr)
        {
            if (seen.count(head))
            {
                return head;
            }
            seen.insert(head);
            head = head->next;
        }
        return nullptr;
    }
};

解法二:双指针

class Solution {
public:
    // 注意快慢指针相遇只能说明链表中有环存在,但是相遇的结点不一定是环节点的第一个结点
    ListNode *detectCycle(ListNode *head) {
        ListNode* fast = head;
        ListNode* slow = head;
        while (true) {
            if (fast == nullptr || fast -> next == nullptr) return nullptr;
            fast = fast -> next;
            if (fast != nullptr) {
                fast = fast -> next;
            }
            slow = slow -> next;
            if (fast == slow) {
                break;
            }
        }
        fast = head;
        while (fast != slow) {
            fast = fast -> next;
            slow = slow -> next;
        }
        return slow;        
    }
};

5. LeetCode 25. K个一组翻转链表

解法一:栈

class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        if (head == nullptr) return nullptr;
        stack<ListNode*> st;
        ListNode* dummy = new ListNode(0);
        dummy->next = head;
        ListNode* current = dummy;
        ListNode* next = dummy->next;
        while (next != nullptr)
        {
            for (int i = 0; i < k && next != nullptr; i++)
            {
                st.push(next);
                next = next->next;
            }
            if (st.size() != k) return dummy->next;
            while (st.size() != 0)
            {
                current->next = st.top();
                st.pop();
                current = current->next;
            }
            current->next = next;
        }
        return dummy->next;
    }
};

解法二:尾插法

class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode *temp = head;
        for (int i = 0; i < k; i++) {
            if (temp == nullptr) {
                return head;
            }
            temp = temp->next;
        }
        ListNode *pre = reverseKGroup(temp, k);
        for (int i = 0; i < k; i++) {
            temp = head->next;
            head->next = pre;
            pre = head;
            head = temp;
        }
        return pre;
    }
};

6. LeetCode 160. 相交链表

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if(headA == nullptr || headB == nullptr) return nullptr;

        ListNode* a = headA;
        ListNode* b = headB;

        while (a != b)
        {
            a = a == nullptr ? headB : a->next;
            b = b == nullptr ? headA : b->next;
        }

        return a;
    }
};