1 import java.util.*; 2 3 /* 4 * public class ListNode { 5 * int val; 6 * ListNode next = null; 7 * public ListNode(int val) { 8 * this.val = val; 9 * } 10 * } 11 */ 12 public class Solution { 13 /** 14 * @param head ListNode类 15 * @return ListNode类 16 */ 17 public ListNode deleteDuplicates (ListNode head) { 18 // 判空链表 19 if (head == null) { 20 return null; 21 } 22 // 增加临时表头 23 ListNode node = new ListNode(0); 24 node.next = head; 25 // 申请遍历指针 26 ListNode cur = node; 27 // 遍历链表 28 while (cur.next != null && cur.next.next != null) { 29 if (cur.next.val == cur.next.next.val) { 30 // 相邻两个节点值相同 31 int deleteValue = cur.next.val; 32 // 将所有相同的都跳过 33 while (cur.next != null && cur.next.val == deleteValue) { 34 cur.next = cur.next.next; 35 } 36 } else { 37 // 相邻两个节点值不同 38 cur = cur.next; 39 } 40 } 41 // 返回时删除表头 42 return node.next; 43 } 44 }
标签:ListNode,cur,删除,16,next,链表,val,null From: https://www.cnblogs.com/StringBuilder/p/17839837.html