2017.11.1
果然一遇到链表就剪不清理还乱。一会儿就彻底懵了。
大致思路就是先判断有没有重复的,
如果有重复的,就全部删除
如果没有重复的,就下一个。
呵呵,道理我都懂啊,我就是写不出来代码啊
/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/*
* @param head: head is the head of the linked list
* @return: head of the linked list
*/
public static ListNode deleteDuplicates(ListNode head) {
// write your code here
if(head == null ||head.next == null){
return head;
}
ListNode myHead = new ListNode(-1);
myHead.next = head;
//表示当前遍历节点的上一个节点
ListNode flag = myHead;
//当前的值
while(head!= null){
int tmp = head.val;
if(head.next == null || head.next.val != tmp){
flag.next = head;
flag = head;
head = head.next;
continue;
}
while(head != null && head.val == tmp){
head = head.next;
}
flag.next = head;
}
return myHead.next;
}
}