剑指offer——删除链表中重复的节点

题目描述

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

方法一:三个指针进行删除:
 

package com.sunny.offer;

/*class ListNode {
	int val;
	ListNode next = null;

	ListNode(int val) {
		this.val = val;
	}
}*/

public class lianbiao2 {

	public static void main(String[] args) {
		/*ListNode head = new ListNode(2);
		ListNode head2 = new ListNode(3);
		ListNode head3 = new ListNode(3);
		ListNode head4 = new ListNode(4);
		ListNode head5 = new ListNode(5);
		ListNode head6 = new ListNode(5);
		ListNode head7 = new ListNode(1);
		head.next = head2;
		head2.next = head3;
		head3.next = head4;
		head4.next = head5;
		head5.next = head6;
		//head6.next = head7;

		ListNode head8 = deleteDuplication(head);
		System.out.println(11);*/
	}

	/*public static ListNode deleteDuplication(ListNode pHead) {
		ListNode p = pHead;
		ListNode i = null;
		ListNode j = null;
		ListNode h = null;
 		while (p.next!=null&&p.val == p.next.val) {
			while (p.val == p.next.val) {
				p = p.next;
				if(p.next == null){
					return null;
					
				}
			}
			p = p.next;

		}
		h = p;

		if (p.next == null) {

			return h;
		}

		i = p.next;
		if (i.next == null) {
			return h;
		}
		j = i.next;
		boolean flag = false;
		while (j != null) {
			flag = false;
			while (j != null) {
				if (i.val == j.val) {
					j = j.next;
					flag = true;
				} else {
					break;
				}
			}
			if (flag == true) {
				p.next = j;
				i = j;
				if (j ==null||j.next == null) {
					return h;
				} else {
					j = j.next;
				}

			} else {

				p = i;
				i = j;
				j = j.next;
			}

		}

		return h;

	}*/

}

方法二:利用hashset进行删除:
 

/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
import java.util.HashSet;
public class Solution {
    public ListNode deleteDuplication(ListNode pHead)
    {
		HashSet<Integer> map = new HashSet<Integer>();
		ListNode yuan = pHead;
		while (pHead != null) {
			int len1 = map.size();
			map.add(pHead.val);
			int len2 = map.size();
			if (len1 == len2) {
				pHead = xiangtong(pHead.val, yuan);
				yuan = pHead;
				map.clear();
				continue;
			}
			pHead = pHead.next;
		}
		
		return yuan;
    }
    	public static ListNode xiangtong(int pHead, ListNode yuan) {
		ListNode first = yuan;
		ListNode end = yuan;
		if (yuan == null) {
			return null;
		}
		boolean flag= false;
		while(first.val == pHead){
			first = first.next;
			if(first == null){
				flag = true;
				break;
				
			}
			
			flag = true;
		}
		if(flag == true){
			return first;
		}

		while (end != null) {
			if (end.val == pHead) {
				end = end.next;
				first.next = end;
				
			} else {
				first = end;
				end = end.next;
			}

		}
		return yuan;



	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值