题解 | #删除链表中重复的结点#
删除链表中重复的结点
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 { // write code here if (pHead === null) return null let root = new ListNode(0) root.next = pHead let current = root let next1 = current.next let next2 if (next1) { next2 = next1.next } while (next1 && next2) { if (next1.val === next2.val) { const tempVal = next1.val while (next2 && tempVal === next2.val) { next2 = next2.next } current.next = next2 next1 = next2 if (next1) { next2 = next1.next } } else { current = next1 next1 = next2 if (next1) { next2 = next1.next } } } return root.next }