categories: DataStructure
/**
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
83. 删除排序链表中的重复元素
问题描述
问题分析
代码实现
class Solution {
public ListNode deleteDuplicates(ListNode head) {
// TODO: 快慢指针
if (head == null) return head;
ListNode slow = head;
ListNode fast = head.next;
while (fast != null){
if (slow.val != fast.val) {
slow.next = fast;
slow = fast;
}
fast = fast.next;
}
slow.next = null;
return head;
}
}
// TODO: 单指针下一步
//class Solution {
// public ListNode deleteDuplicates(ListNode head) {
// ListNode cur = head;
// while(cur != null && cur.next != null) {
// if(cur.val == cur.next.val) {
// cur.next = cur.next.next;
// } else {
// cur = cur.next;
// }
// }
// return head;
// }
//}