题解 | #删除链表中重复的结点#
删除链表中重复的结点
https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef
/*class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pHead ListNode类 * @return ListNode类 */ export function deleteDuplication(pHead: ListNode): ListNode { // 加一个表头比较好操作一些 //1.当遇到一个节点等于下一个节点进行跳过去,连接不相等的节点 //2.否则就连上 if(pHead === null)return null let res = new ListNode(0) res.next = pHead //记录一下当前的表头 let current = res //判断下面的两个节点 while(current.next !== null && current.next.next !== null){ if(current.next.val === current.next.next.val){ const temp = current.next.val //将所有相同的节点都跳过 while(current.next !== null && current.next.val === temp){ current.next = current.next.next } }else{ current = current.next } } return res.next }