题解 | #删除有序链表中重复的元素-I#
删除有序链表中重复的元素-I
http://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 fast = head.next;
fast = fast.next;
}
slow.next = fast;
slow = slow.next;
if(fast!=null){
fast = fast.next;
}
}
return head;
}
}
/*
* 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 slow = head;ListNode fast = head.next;
while(fast!=null){
//fast移动到不等于slow的节点
while(fast!=null&&fast.val==slow.val){fast = fast.next;
}
slow.next = fast;
slow = slow.next;
if(fast!=null){
fast = fast.next;
}
}
return head;
}
}