[剑指offer]删除链表中重复的结点
Posted moonbeautiful
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[剑指offer]删除链表中重复的结点相关的知识,希望对你有一定的参考价值。
题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
package com.sunshine.OFFER66_SECOND; import org.junit.Test; public class A56_deleteDuplication { @Test public void test() { ListNode n1 = new ListNode(1); ListNode n2 = new ListNode(1); ListNode n3 = new ListNode(2); ListNode n4 = new ListNode(3); ListNode n5 = new ListNode(3); ListNode n6 = new ListNode(4); ListNode n7 = new ListNode(5); ListNode n8 = new ListNode(5); n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n5; n5.next = n6; n6.next = n7; n7.next = n8; ListNode pos = n1; while (pos != null) { System.out.print(pos.val + "->"); pos = pos.next; } System.out.println(); ListNode listNode = deleteDuplication(n1); pos = listNode; while (pos != null) { System.out.print(pos.val + "->"); pos = pos.next; } } //其他人解 public ListNode deleteDuplication(ListNode pHead) { if (null == pHead || null == pHead.next) { return pHead; } ListNode pre = new ListNode(0); ListNode ans = pre; pre.next = pHead; ListNode cur = pHead; while (cur != null) { if (cur.next != null && cur.next.val == cur.val) { while (cur.next != null && cur.next.val == cur.val) { cur = cur.next; } cur = cur.next; pre.next = cur; } else { pre = cur; cur = cur.next; } } return ans.next; } }
以上是关于[剑指offer]删除链表中重复的结点的主要内容,如果未能解决你的问题,请参考以下文章