删除链表中重复的节点
题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留。
样例 1
输入:1->2->3->3->4->4->5
输出:1->2->5
样例 2
输入:1->1->1->2->3
输出:2->3
解法
解法一:递归
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
/**
* 删除链表重复的节点
*
* @param head 链表头节点
* @return 删除重复节点后的链表
*/
public ListNode deleteDuplication(ListNode head) {
if (head == null || head.next == null) {
return head;
}
if (head.next.val == head.val) {
if (head.next.next == null) {
return null;
}
if (head.next.next.val == head.val) {
return deleteDuplication(head.next);
}
return deleteDuplication(head.next.next);
}
head.next = deleteDuplication(head.next);
return head;
}
}
解法二:非递归
pre 始终指向下一个不重复的节点。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
/**
* 删除链表重复的节点
*
* @param head 链表头节点
* @return 删除重复节点后的链表
*/
public ListNode deleteDuplication(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode pre = null, cur = head;
while (cur != null) {
if (cur.next != null && cur.next.val == cur.val) {
int val = cur.val;
while (cur.next != null && cur.next.val == val) {
cur = cur.next;
}
if (pre == null) {
head = cur.next;
} else {
pre.next = cur.next;
}
} else {
pre = cur;
}
cur = cur.next;
}
return head;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

上一篇: 在 O(1) 时间删除链表节点
下一篇: 彻底找到 Tomcat 启动速度慢的元凶
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论