(1) Q: Remove duplicate ordered list of nodes.
Ideas: a difficulty is not known where the initial node on the results, the initial node because there may be duplicate nodes, a method is therefore provided a blank node ListNode first = new ListNode (-1).
Setting a node ListNode last = first used for the deletion node operation. Set node ListNode p = pHead; to traverse the list.
Traversing the list, the same value val val p value and the next node if the node p junction point, the loop will be at a p junction node points to the different values, last.next = p.
If the value val val p value and the next node node different from the node p, the point p junction point represents the last node does not move.
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode deleteDuplication(ListNode pHead)
{
if(pHead == null||pHead.next == null){
return pHead;
}
ListNode first =new ListNode(-1);
first.next = pHead;
ListNode last = first;
ListNode p = pHead;
while(p != null && p.next != null){
if(p.next.val != p.val){
last = p;
p = p.next;
}
else{
int val = p.val;
while(p != null && p.val == val){
p = p.next;
}
last.next = p;
}
}
return first.next;
}
}