题解 | #删除有序链表中重复的元素-I#
删除有序链表中重复的元素-I
https://www.nowcoder.com/practice/c087914fae584da886a0091e877f2c79
import java.util.*;
/*
* 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 newHead = new ListNode(-1);
ListNode node = newHead;
node.next = head;
head = head.next;
node = node.next;
node.next = null;
while (head != null) {
if (head.val == node.val) {
head = head.next;
} else {
node.next = head;
head = head.next;
node = node.next;
node.next = null;
}
}
return newHead.next;
}
}
深信服公司福利 896人发布
查看15道真题和解析