侧边栏壁纸
博主头像
阿里灰太狼博主等级

You have to believe in yourself . That's the secret of success.

  • 累计撰写 104 篇文章
  • 累计创建 50 个标签
  • 累计收到 12 条评论

目 录CONTENT

文章目录

leetcode-83. 删除排序链表中的重复元素

阿里灰太狼
2021-11-18 / 0 评论 / 0 点赞 / 98 阅读 / 532 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2021-11-18,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

cj56
cj57

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) {
        // 头节点为空则为空链表
        if (head == null) {
            return head;
        }
        // 当前节点
        ListNode cur = head;
        // 存在节点值相同则跳过
        while (cur.next != null) {
            if (cur.val == cur.next.val) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
        return head;
    }
}

leetcode原题: 83. 删除排序链表中的重复元素

解法分析

先对传进来的链表头节点判空,为空则直接返回头节点,非空则对当前节点与下一个节点进行值的判断,若不相等则把当前节点变成下一个节点,若相等则跳过。

0

评论区