删除给出链表中的重复元素(链表中元素从小到大有序),使链表中的所有元素都只出现一次
例如:
给出的链表为,返回.
给出的链表为,返回.
例如:
给出的链表为,返回.
给出的链表为,返回.
数据范围:链表长度满足 ,链表中任意节点的值满足
进阶:空间复杂度 ,时间复杂度
public class Solution { public ListNode deleteDuplicates (ListNode head) { ListNode dummyHead = new ListNode(-101); dummyHead.next = head; ListNode pre = dummyHead; ListNode p = dummyHead.next; HashSet<Integer> set = new HashSet<>(); while (p != null) { if (!set.contains(p.val)) { set.add(p.val); pre = p; p = p.next; } else { pre.next = p.next; p = p.next; if (p != null && p.val != pre.val) { set.add(p.val); pre = p; p = p.next; } } } return dummyHead.next; } }
public ListNode deleteDuplicates (ListNode head) { if(head == null || head.next == null) return head; ListNode pre = head,last = head.next; while(last != null){ if(pre.val == last.val){ pre.next = last.next; last = pre.next; }else{ last = last.next; pre = pre.next; } } return head; }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ public ListNode deleteDuplicates (ListNode head) { // write code here // 算法的时间复杂度O(N),额外空间复杂度O(1) // 1.处理特殊链表 if (head == null || head.next == null) { // 对于空链表,单结点链表,直接返回head return head; } // 2.遍历链表,每次定位重复结点的最后一个结点,跳过中间结点 ListNode cur = head; ListNode dupEnd = null; while (cur != null) { // 对于任一cur结点,哪怕没有重复,dupEnd至少是cur自己 dupEnd = cur; while(dupEnd.next != null && dupEnd.val == dupEnd.next.val) { // 如果发现重复,dupEnd往后移动一位 dupEnd = dupEnd.next; } // 循环结束时,dupEnd指向重复结点的最后一个,且不会是null // 跳连 + 移动 cur.next = dupEnd.next; cur = cur.next; } // 3.返回头部 return head; } }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ public ListNode deleteDuplicates (ListNode head) { // write code here if (head==null||head.next==null) return head; ListNode p=null,q=null; q=head;p=head.next; while(p!=null) { if(q.val==p.val) { p=p.next; q.next=p; } else{ q=p; p=p.next; } } return head; } }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ public ListNode deleteDuplicates (ListNode head) { // write code here if(head == null || head.next == null){ return head; } ListNode pre = new ListNode(-1); pre.next = head; while(head != null && head.next != null){ if(head.val == head.next.val){ head.next = head.next.next; }else{ head = head.next; } } return pre.next; } }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ public ListNode deleteDuplicates (ListNode head) { // write code here if(head == null || head.next == null) return head; ListNode pre = head; ListNode cur = head.next; while (cur != null) { if (pre.val == cur.val){ pre.next = cur.next; cur = cur.next; } else { pre.next = cur; pre = pre.next; cur = cur.next; } } return head; } }
public ListNode deleteDuplicates (ListNode head) { // 删除重复元素,链表有序 ListNode dummy=new ListNode(-1); dummy.next=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 dummy.next; }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ public ListNode deleteDuplicates (ListNode head) { // write code here //1、判断头节点是否为空 if (head == null) { return null; } //2、构造一个虚拟头节点 ListNode newHead = new ListNode(-1); ListNode cur = head; ListNode tmp = newHead; //3、遍历链表 while (cur != null) { if (cur.next != null && cur.val == cur.next.val) { while (cur.next != null && cur.val == cur.next.val) { cur = cur.next; } } else { tmp.next = cur; tmp = tmp.next; cur = cur.next; } } tmp.next = null; return newHead.next; } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ public ListNode deleteDuplicates (ListNode head) { // 遍历head Set<Integer> set = new LinkedHashSet<>(); while (head != null) { set.add(head.val); head = head.next; } ListNode result = null; ListNode current = null; int count = 0; // 遍历set集合 for (Integer s : set) { if (count == 0) { // 第一个节点 current = new ListNode(s); // 记录下第一个节点 result = current; } else { ListNode next = new ListNode(s); // 将下一个节点加进来 current.next = next; // 移动头节点到下一个节点 current = current.next; // System.out.println(s); } count++; } return result; }
public class Solution { /** * * 单子指针 */ public ListNode deleteDuplicates (ListNode head) { if(head == null || head.next == null) return head; ListNode fore = head; while(fore.next != null){ if(fore.val == fore.next.val){ fore.next =fore.next.next; }else{ fore = fore.next; } } return head; } //双指针 public ListNode deleteDuplicates (ListNode head) { if(head == null || head.next == null) return head; ListNode fore = head,back = head.next; while(back != null){ if(fore.val == back.val){ fore.next = back.next; back = back.next; }else{ fore = fore.next; back = back.next; } } return head; } }
import java.util.*; public class Solution { public ListNode deleteDuplicates (ListNode head) { if(head == null){ return null; } //相同跳过,不相等继续走 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; } }
public class Solution { /** * * @param head ListNode类 * @return ListNode类 */ public ListNode deleteDuplicates (ListNode head) { // write code here if (head == null) return null; List<Integer> list = new ArrayList(); while (head != null) { list.add(head.val); head = head.next; } List<Integer> distinctList = list.stream().distinct().collect( Collectors.toList()); ListNode sentinel = new ListNode(-1); ListNode cur = sentinel; for (int i = 0; i < distinctList.size(); i++) { cur.next = new ListNode(distinctList.get(i)); cur = cur.next; } return sentinel.next; } }