题解 | #删除有序链表中重复的元素-I#
删除有序链表中重复的元素-I
http://www.nowcoder.com/practice/c087914fae584da886a0091e877f2c79
思路:双指针法,一个指向当前节点,一个指向下一个不等于当前节点的节点。时间击败100%
代码:
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 cur=head;
ListNode curNext=cur.next;
while(cur!=null){
while(curNext!=null&&cur.val==curNext.val){
curNext=curNext.next;
}
cur.next=curNext;
cur=cur.next;
}
return head;
}
}