Skip to content

Commit

Permalink
Merge pull request #1040 from lhj8/main
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Oct 21, 2024
2 parents c597cc5 + b897020 commit 43ef4ce
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 0 deletions.
40 changes: 40 additions & 0 deletions hj/week3/CopyLinkedListWithRandomPointer.cpp
Original file line number Diff line number Diff line change
@@ -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<Node*, Node*> 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];
}
};
36 changes: 36 additions & 0 deletions hj/week3/MergeTwoSortedLinkedLists.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
32 changes: 32 additions & 0 deletions hj/week3/RemoveNodeFromEndofLinkedList .cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
46 changes: 46 additions & 0 deletions hj/week3/ReorderLinkedList.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
}
};
29 changes: 29 additions & 0 deletions hj/week3/ReverseALinkedList.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};

0 comments on commit 43ef4ce

Please sign in to comment.