题解 | #删除有序链表中重复的元素-I#
删除有序链表中重复的元素-I
http://www.nowcoder.com/practice/c087914fae584da886a0091e877f2c79
/*
* 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 (null == head) {
return null;
}
if (null == head.next) {
return head;
}
// 具体代码如下
// 定义一个指针
ListNode p = null;
// 定义一个辅助节点
ListNode tmp = null;
// 初始化
p = head.next;
tmp = head;
while (null != p) {
if (p.val != tmp.val) {
p = p.next;
tmp = tmp.next;
}
else {
tmp.next = p.next;
p = p.next;
}
}
return head;
}
}