Appearance
2095删除链表中间节点
code
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 deleteMiddle = function(head) {
if(head === null || head.next === null){
return null;
}
let ind_fast = head;
let ind_slow = head;
let temp;
while(ind_fast !== null && ind_fast.next !== null){
ind_fast = ind_fast.next.next;
temp = ind_slow;
ind_slow = ind_slow.next;
}
if(temp.next.next === null){
console.log('进');
temp.next = null;
} else {
temp.next = temp.next.next;
}
return head;
};总结
- 好像也没啥稀奇的,就是个快慢指针,跟找链表中间节点差不多
- 说到底还是各种边界情况问题,其实感觉acm线上可能还好,但是如果是线下手写的话很容易就被搞了