李恒道 发表于 2024-8-26 17:42:37

https://leetcode.cn/problems/merge-two-sorted-lists/submissions/558715098/?envType=study-plan-v2&envId=top-100-liked
简单题,链表合并
```js
var mergeTwoLists = function (list1, list2) {
    let head = { next: null }
    const ret = head
    while (list1 != null && list2 != null) {
      if (list1.val < list2.val) {
            head.next = list1
            list1 = list1.next
            head = head.next
      } else {
            head.next = list2
            list2 = list2.next
            head = head.next
      }
    }


    do {
      if (list2) {
            head.next = list2
            list2 = list2.next
            head = head.next
      }
      if (list1) {
            head.next = list1
            list1 = list1.next
            head = head.next
      }

    } while (list1 != null || list2 != null);
    return ret.next
};
```

李恒道 发表于 2024-8-26 17:42:59

王一之 发表于 2024-8-26 16:57
我提议新开一个catcode平台,重新进行排名

这样我就可以给自己改成第一了!

李恒道 发表于 2024-8-26 18:02:37

https://leetcode.cn/problems/add-two-numbers/submissions/558720843/?envType=study-plan-v2&envId=top-100-liked
简单题
```js
var addTwoNumbers = function (l1, l2) {
    let add = 0
    let head = {
      next: null
    }
    let ret = head
    while (l1 != null || l2 != null) {
      const num1 = l1?.val ?? 0
      const num2 = l2?.val ?? 0
      let total = num1 + num2 + add
      add =0
      if(total>=10){
            total-=10
            add=1
      }
      head.next = {
            val: total,
            next: null
      }
      head = head.next
      l1 = l1?.next;
      l2 = l2?.next
    }
    if (add !== 0) {
      head.next = {
            val: add,
            next: null
      }
    }
    return ret.next
};
```

李恒道 发表于 2024-8-26 18:23:04

https://leetcode.cn/problems/remove-nth-node-from-end-of-list/submissions/558725242/?envType=study-plan-v2&envId=top-100-liked
边界有点难控制,其他没了
```js
var removeNthFromEnd = function (head, n) {
    if(head.next==null&&n==1){
      return null
    }
    let fast = head
    let slow = head
    for (let index = 0; index < n; index++) {
      fast = fast.next
    }
    if(fast==null){
      return head.next
    }
    while (fast?.next != null) {
      slow = slow.next
      fast = fast.next
    }
    slow.next=slow?.next?.next??null
    return head
};
```

李恒道 发表于 2024-8-26 18:38:24

https://leetcode.cn/problems/swap-nodes-in-pairs/submissions/558728046/?envType=study-plan-v2&envId=top-100-liked
简单交换题
```js
    var swapPairs = function (head) {
      if(head?.next==null){
            return head
      }
      const ret = {
            next:null
      }
      let setHead=ret

      while (head?.next != null) {
            let first = head
            let last = head.next
            head = last.next
            first.next = last.next;
            last.next = first
            setHead.next=last
            setHead=first
      }
      return ret.next
    };
```

李恒道 发表于 2024-8-26 18:49:59

https://leetcode.cn/problems/number-of-steps-to-reduce-a-number-to-zero/submissions/558730132/
简单题水一下换换心情
```js
var numberOfSteps = function (num) {
    let times = 0
    while (num != 0) {
      num = num % 2 == 0 ? num / 2 : num - 1;
      times++
    }
    return times
};
```

李恒道 发表于 2024-8-27 05:13:42

https://leetcode.cn/problems/reverse-nodes-in-k-group/submissions/558836129/?envType=study-plan-v2&envId=top-100-liked
还算可以
边界判断蒙了
```js
var reverseKGroup = function (head, k) {
let fast = head;
let slow = head;
let ret = { next: null };
let chain = ret;
while (slow != null) {
    let isJump=false
    for (let index = 0; index < k; index++) {
      fast = fast.next;
      if (fast == null) {
      if(index!=k-1){
            isJump=true
      }
      break;
      }
    }
    if (isJump) {
      chain.next = slow;
      break;
    }
    let last = null;
    let first = slow;
    while (slow != fast) {
      let next = slow.next;
      slow.next = last;
      last = slow;
      slow = next;
    }
    chain.next = last;
    chain = first;
}

return ret.next;
};
```

李恒道 发表于 2024-9-3 02:14:06

https://leetcode.cn/problems/copy-list-with-random-pointer/submissions/560906494/?envType=study-plan-v2&envId=top-100-liked
凹了一晚上才凹出来
思路歪了...
```js
var copyRandomList = function (head) {
    if(head==null){
      return null
    }
    let chainPoint = head
    do {
      const val = chainPoint.val
      const next = chainPoint.next
      const newNode = {
            val,
            next
      }
      chainPoint.next = newNode
      chainPoint = chainPoint?.next?.next
    } while (chainPoint != null);
    let coptPoint = head
    while (coptPoint != null) {
      coptPoint.next.random = coptPoint?.random?.next ?? null
      coptPoint = coptPoint?.next?.next
    }
    let newChain = {
      next: null
    }
    const ret = newChain
    while (head != null) {
      newChain.next = head.next;
      newChain = newChain.next
      head.next = head?.next?.next ?? null
      head = head.next
    }
    return ret.next
};
```

李恒道 发表于 2024-9-3 04:02:45

https://leetcode.cn/problems/sort-list/?envType=study-plan-v2&envId=top-100-liked
打一波归序,但是快慢指针控制的不好
边界过多了
```js
var sortList = function (head, end = undefined) {
    if (head?.next == end) {
      return head
    }
    let slow = head?.next;//第二段开头
    let first = head;//第一段结尾
    let fast = head?.next;//第二个结尾
    if (fast != end) {
      fast = fast?.next
      if (fast?.next != end) {
            while (fast != end) {
                slow = slow?.next;
                fast = fast?.next?.next;
                if (fast?.next == end) {
                  fast = fast?.next
                  break;
                }
            }
      }else{
            fast = fast?.next
      }
    }

    let china1 = sortList(first, slow)
    let china2 = sortList(slow, end)
    let headptr = {
      next: null
    }
    const ptr = headptr
    while (china1 != slow || china2 != fast) {
      if (china1 != slow && china2 != fast) {
            const num1 = china1?.val;
            const num2 = china2?.val;
            if (num1 < num2) {
                headptr.next = china1
                china1 = china1.next;
            } else {
                headptr.next = china2
                china2 = china2.next;
            }
      } else {
            headptr.next = china1 != slow ? china1 : china2
            if (china1 != slow) {
                china1 = china1.next;
            } else {
                china2 = china2.next;
            }
      }
      headptr = headptr.next
    }
    headptr.next = end ?? null
    return ptr.next

};
```

李恒道 发表于 2024-9-3 04:05:52

https://leetcode.cn/problems/middle-of-the-linked-list/submissions/560909018/
简单题直接秒
```js
var middleNode = function (head) {
    let slow = head
    let fast = head
    while (fast != null&&fast.next!=null) {
      fast = fast?.next;
      slow = slow.next
      fast = fast?.next;
    }
    return slow
};
```
页: 1 2 3 4 5 [6] 7 8 9 10 11 12 13 14 15
查看完整版本: 【当前排名67359】挑战leetcode进入前1w名