删除链表中重复的结点

来源:互联网 发布:乐知在线英语免费 编辑:程序博客网 时间:2024/06/09 08:15

题目描述

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

具体代码如下:

/* public class ListNode {    int val;    ListNode next = null;    ListNode(int val) {        this.val = val;    }}*/public class Solution {    public ListNode deleteDuplication(ListNode pHead)    {        if (pHead == null) return null;        ListNode p = pHead;        ListNode n = new ListNode(0);        ListNode pre = n;        n.next = pHead;        boolean flag = false;        while (p != null) {            ListNode q = p.next;            if (q == null) break;            if (q.val == p.val) {                while (q != null && q.val == p.val) {                    q = q.next;                }                pre.next = q;                p = q;            } else {                if (!flag) {                    n.next = p;                    flag = true;                }                pre = p;                p = q;            }        }        return n.next;    }}
原创粉丝点击