题解 | #删除链表中重复的结点#
删除链表中重复的结点
http://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef
/*
public class ListNode
{
public int val;
public ListNode next;
public ListNode (int x)
{
val = x;
}
}*/
class Solution
{
public ListNode deleteDuplication(ListNode pHead)
{
// write code here
if (pHead == null) return pHead;
ListNode iter = pHead;
ListNode newHead = new ListNode(0);
ListNode cur = newHead;
while(iter!=null){
if (iter.next==null || iter.val != iter.next.val){
cur.next = iter;
cur = cur.next;
}else{
while(iter.next!=null && iter.val == iter.next.val){
iter = iter.next;
}
}
iter = iter.next;
}
cur.next = null;
return newHead.next;
}
}
public class ListNode
{
public int val;
public ListNode next;
public ListNode (int x)
{
val = x;
}
}*/
class Solution
{
public ListNode deleteDuplication(ListNode pHead)
{
// write code here
if (pHead == null) return pHead;
ListNode iter = pHead;
ListNode newHead = new ListNode(0);
ListNode cur = newHead;
while(iter!=null){
if (iter.next==null || iter.val != iter.next.val){
cur.next = iter;
cur = cur.next;
}else{
while(iter.next!=null && iter.val == iter.next.val){
iter = iter.next;
}
}
iter = iter.next;
}
cur.next = null;
return newHead.next;
}
}