题解 | #删除有序链表中重复的元素-I#TOP15
思路:
1.对于当前节点,利用while循环删掉后面值和他相等的节点即可
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode deleteDuplicates (ListNode head) {
// write code here
if(head == null || head.next == null){
return head;
}
ListNode currentNode = head;
while(currentNode != null){
ListNode next = currentNode.next;
while(next != null && currentNode.val == next.val){
currentNode.next = next.next;
next = next.next;
}
currentNode = currentNode.next;
}
return head;
}
}
面试必刷TOP101 文章被收录于专栏
面试必刷TOP101