题解 | #删除链表中重复的结点#

删除链表中重复的结点

http://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef

/*
public class ListNode
{
    public int val;
    public ListNode next;
    public ListNode (int x)
    {
        val = x;
    }
}*/
class Solution
{
    public ListNode deleteDuplication(ListNode pHead)
    {
        // write code here
        if (pHead == null) return pHead;
        ListNode iter = pHead;
        ListNode newHead = new ListNode(0);
        ListNode cur = newHead;
        while(iter!=null){
            if (iter.next==null || iter.val != iter.next.val){
                cur.next = iter;
                cur = cur.next;
            }else{
                while(iter.next!=null && iter.val == iter.next.val){
                    iter = iter.next;
                }
            }
            iter = iter.next;
        }
        cur.next = null;
        return newHead.next;
    }
}
全部评论

相关推荐

06-12 16:00
天津大学 Java
牛客30236098...:腾讯坏事做尽,终面挂是最破防的 上次被挂了后我连简历都不刷了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务