标签:turn 数字 while targe list get remove str this
Difficulty: 中等
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 _没有重复出现 _的数字。
示例 1:
输入: 1->2->3->3->4->4->5
输出: 1->2->5
示例 2:
输入: 1->1->1->2->3
输出: 2->3
Language: java
?/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode pre = null, p = head, newHead= null;
while(p != null){
if(p.next != null && p.next.val == p.val){
if(pre != null) pre.next = null;
while(p.next != null && p.next.val == p.val){
ListNode t = p;
p = p.next;
t.next = null;
}
}else{
if(pre == null) newHead = p;
else pre.next = p;
pre = p;
}
p = p.next;
}
return newHead;
}
}
标签:turn 数字 while targe list get remove str this
原文地址:https://www.cnblogs.com/liuyongyu/p/14356780.html