var swapPairs = function(head) { // Initialise a dummy list containing the provided one let dummyList = new ListNode(null, head); // Create a copy of the dummy list which we can traverse with let current = dummyList; // While there are 2 additional elements remaining while (current.next && current.next.next) { // Obtain the nodes to be swapped const first = current.next; const second = current.next.next; // Swap the nodes first.next = second.next; second.next = first; current.next = second; // Move forward by 2 elements current = current.next.next; } // Return the swapped LinkedList, removing the dummy head return dummyList.next; };
var minSubArrayLen = function(target, nums) { let l = 0; let r = 0; let ans = Infinity; const n = nums.length; let total = 0; while (l < n) { while(r < n && total < target) { total += nums[r]; r++; } if (total < target) break; ans = Math.min(ans, r - l); total -= nums[l]; l++; } return ans === Infinity ? 0 : ans; };
var reverseBetween = function(head, left, right) { let dummy = new ListNode(); let node = dummy; node.next = head;
for(let i = 1; i < left; i++) { node = node.next; }
let curr = node.next; let next = curr.next;for(let i = 0; i < right - left; i++) { curr.next = next.next; next.next = node.next; node.next = next;next = curr.next; }
網路請求路由:架構完整的專案中,可能會提供 development 版本的 API 與 production 版本的 API,通常兩種版本的 API 也會部署在不同的 domain 上面。但我們不希望在 Client Side 處理決定呼叫哪個 API 的邏輯,可以由 proxy 決定呼叫哪個 API。