题解 | #删除链表中重复的结点#
删除链表中重复的结点
https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef
import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
//递归
public ListNode deleteDuplication(ListNode pHead) {
if(pHead==null || pHead.next == null){
return pHead;
}
ListNode next = pHead.next;
if(pHead.val == next.val){
while(next != null && pHead.val == next.val){
next=next.next;
}
pHead = deleteDuplication(next);
}
else{
pHead.next = deleteDuplication(next);
}
return pHead;
}
}
查看20道真题和解析