Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
,
return 1->2
.
Given 1->1->2->3->3
,
return 1->2->3
.
【分析-原创】相等的话nextNode后移即可,不相等的话currentNode和nextNode都后移。
<span style="font-weight: normal;">/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode deleteDuplicates(ListNode head) { if(head==null||head.next==null) return head; ListNode currentNode=head; ListNode nextNode=null; while(currentNode.next!=null){ nextNode=currentNode.next; if(currentNode.val==nextNode.val){ nextNode=nextNode.next; currentNode.next=nextNode; //currentNode=nextNode; }else{ currentNode=nextNode; nextNode=nextNode.next; } } return head; } }</span>
【LeetCode题目记录-8】从排序后的单链表中去除重复元素
原文地址:http://blog.csdn.net/brillianteagle/article/details/39295055