From 1d4b1e0e9b71e2ea629ad4cd88b85b7ed66da367 Mon Sep 17 00:00:00 2001 From: Klaiden Date: Tue, 15 Oct 2024 19:45:05 -0700 Subject: [PATCH 1/2] week 3 --- hj/week3/MergeTwoSortedLinkedLists.cpp | 36 ++++++++++++++++ hj/week3/RemoveNodeFromEndofLinkedList .cpp | 32 ++++++++++++++ hj/week3/ReorderLinkedList.cpp | 46 +++++++++++++++++++++ hj/week3/ReverseALinkedList.cpp | 29 +++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 hj/week3/MergeTwoSortedLinkedLists.cpp create mode 100644 hj/week3/RemoveNodeFromEndofLinkedList .cpp create mode 100644 hj/week3/ReorderLinkedList.cpp create mode 100644 hj/week3/ReverseALinkedList.cpp diff --git a/hj/week3/MergeTwoSortedLinkedLists.cpp b/hj/week3/MergeTwoSortedLinkedLists.cpp new file mode 100644 index 00000000..3eba8efd --- /dev/null +++ b/hj/week3/MergeTwoSortedLinkedLists.cpp @@ -0,0 +1,36 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ + +class Solution { +public: + ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { + if (!list1 && !list2) return nullptr; + + ListNode* dummy = new ListNode(0); + ListNode* head = dummy; + + while(list1 && list2) { + if (list1->val < list2->val) { + dummy->next = list1; + list1 = list1->next; + } + else { + dummy->next = list2; + list2 = list2->next; + } + dummy = dummy->next; + } + + dummy->next = list1 ? list1 : list2; + + return head->next; + } +}; diff --git a/hj/week3/RemoveNodeFromEndofLinkedList .cpp b/hj/week3/RemoveNodeFromEndofLinkedList .cpp new file mode 100644 index 00000000..d808248a --- /dev/null +++ b/hj/week3/RemoveNodeFromEndofLinkedList .cpp @@ -0,0 +1,32 @@ +/** +* Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ + +class Solution { +public: + ListNode* removeNthFromEnd(ListNode* head, int n) { + ListNode* dummy = new ListNode(0, head); + ListNode* r = head; + ListNode* l = dummy; + + while (n > 0) { + r = r->next; + n--; + } + + while (r) { + l = l->next; + r = r->next; + } + + l->next = l->next->next; + return dummy->next; + } +}; diff --git a/hj/week3/ReorderLinkedList.cpp b/hj/week3/ReorderLinkedList.cpp new file mode 100644 index 00000000..a8997e7a --- /dev/null +++ b/hj/week3/ReorderLinkedList.cpp @@ -0,0 +1,46 @@ +/** +* Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + void reorderList(ListNode* head) { + if (!head->next) return; + + ListNode* slow = head; + ListNode* fast = head; + + while (fast && fast->next) { + slow = slow->next; + fast = fast->next->next; + } + + ListNode* prev = nullptr; + ListNode* crnt = slow; + while (cur) { + ListNode* temp = crnt->next; + crnt->next = prev; + prev = crnt; + crnt = temp; + } + + ListNode* l1 = head; + ListNode* l2 = prev; + while (l2->next) { + ListNode* n1 = l1->next; + ListNode* n2 = l2->next; + + l1->next = l2; + l1 = n1; + + l2->next = l1; + l2 = n2; + } + } +}; \ No newline at end of file diff --git a/hj/week3/ReverseALinkedList.cpp b/hj/week3/ReverseALinkedList.cpp new file mode 100644 index 00000000..f6a80a84 --- /dev/null +++ b/hj/week3/ReverseALinkedList.cpp @@ -0,0 +1,29 @@ +/** +* Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ + +class Solution { +public: + ListNode* reverseList(ListNode* head) { + if (!head) return nullptr; + + ListNode* prev = nullptr; + ListNode* crnt = head; + + while (crnt) { + ListNode* temp = crnt->next; + crnt->next = prev; + prev = crnt; + crnt = temp; + } + + return prev; + } +}; From b8970205512bba2d77e0ca01f3d327b485094cb8 Mon Sep 17 00:00:00 2001 From: Klaiden Date: Tue, 15 Oct 2024 19:52:27 -0700 Subject: [PATCH 2/2] week 3 --- hj/week3/CopyLinkedListWithRandomPointer.cpp | 40 ++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 hj/week3/CopyLinkedListWithRandomPointer.cpp diff --git a/hj/week3/CopyLinkedListWithRandomPointer.cpp b/hj/week3/CopyLinkedListWithRandomPointer.cpp new file mode 100644 index 00000000..3d4b833a --- /dev/null +++ b/hj/week3/CopyLinkedListWithRandomPointer.cpp @@ -0,0 +1,40 @@ +/* +// Definition for a Node. +class Node { +public: + int val; + Node* next; + Node* random; + + Node(int _val) { + val = _val; + next = NULL; + random = NULL; + } +}; +*/ + +class Solution { +public: + Node* copyRandomList(Node* head) { + if (!head) return nullptr; + + unordered_map um; + + Node* crnt = head; + while (crnt) { + Node* node = new Node(crnt->val); + um[crnt] = node; + crnt = crnt->next; + } + + crnt = head; + while (crnt) { + um[crnt]->next = um[crnt->next]; + um[crnt]->random = um[crnt->random]; + crnt = crnt->next; + } + + return um[head]; + } +};