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]; + } +}; 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; + } +};