본문 바로가기

Leetcode4

92. Reverse Linked List II Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL Solution /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ #include struct ListNode* reverseBetween(struct ListNode* head, int m, int n){ struct ListNode *dummy = (stru.. 2020. 5. 4.
237. Delete Node in a Linked List node가 tail이 아니라는 가정때문에 가능한 일이다 node의 prev를 찾는 게 아니라 현재 node와 다음 node를 바꾸고 현재 node의 다음을 그 다음으로 바꾼다. Solution : void deleteNode(struct ListNode* node) { node->val = node->next->val; node->next = node->next->next; } 2020. 3. 22.
706. Design HashMap Design a HashMap without using any built-in hash table libraries. To be specific, your design should include these functions: put(key, value) : Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the value. get(key): Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key. remove(key) : Remove the mappi.. 2020. 3. 22.
704. Binary Search 문제 : 정렬된 배열에서 주어진 숫자의 인덱스를 반환하는 함수를 만들어라 Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Solution: //처음부터 그냥 찾으면 Time limit에 걸림 //이진탐색해야함 int search(int* nums, int numsSize, int target){ int l = 0, r = numsSize-1; while(l target) r = pivot-1; else if(nums[pivot] 2020. 3. 21.