一、手写算法
https://leetcode-cn.com/problems/swap-nodes-in-pairs/
思路
- 借助另外一个链表,进行两两交换
代码
```javascript /**- Definition for singly-linked list.
- function ListNode(val, next) {
- this.val = (val===undefined ? 0 : val)
- this.next = (next===undefined ? null : next)
- } / /*
- @param {ListNode} head
- @return {ListNode}
*/
var swapPairs = function(head) {
var newNode = new ListNode(0);
newNode.next = head
var temp = newNode;
while(temp.next !== null && temp.next.next !== null){
} return newNode.next; };var node1 = temp.next;
var node2 = temp.next.next;
temp.next = node2;
node1.next = node2.next;
node2.next = node1;
temp = node1;
<a name="fbzED"></a>
### 复杂度分析
- 时间复杂度:O(N)
- 空间复杂度:O(N)
<a name="Ghcnm"></a>
## 二、编程题
```javascript
2.手写题:https://bigfrontend.dev/zh/problem/implement-your-own-Object-create
/**
* @param {any} proto
* @return {object}
*/
function myObjectCreate(proto) {
// your code here
if(typeof proto !== 'object' && typeof proto !== 'function'){
throw Error(typeof proto)
}else if (proto === null) {
throw Error('null')
}
function F(){};
F.prototype = proto;
return new F();
}