删除链表中反复的结点
题目形容
在一个排序的链表中,存在反复的结点,请删除该链表中反复的结点,反复的结点不保留,返回链表头指针。例如,链表 1 ->2->3->3->4->4->5 解决后为 1->2->5。
题目链接 : 删除链表中反复的结点
代码
/**
* 题目:删除链表中反复的结点
* 题目形容
* 在一个排序的链表中,存在反复的结点,请删除该链表中反复的结点,反复的结点不保留,返回链表头指针。例如,链表 1 ->2->3->3->4->4->5 解决后为 1->2->5
* 题目链接:* https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef?tpId=13&&tqId=11209&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
*/
public class Jz56 {public ListNode deleteDuplication(ListNode pHead) {if (pHead == null || pHead.next == null) {return pHead;}
ListNode next = pHead.next;
if (pHead.val == next.val) {while (next != null && pHead.val == next.val) {next = next.next;}
return deleteDuplication(next);
} else {pHead.next = deleteDuplication(pHead.next);
return pHead;
}
}
public static void main(String[] args) {ListNode pHead = new ListNode(1);
pHead.next = new ListNode(1);
pHead.next.next = new ListNode(1);
pHead.next.next.next = new ListNode(1);
pHead.next.next.next.next = new ListNode(1);
pHead.next.next.next.next.next = new ListNode(1);
pHead.next.next.next.next.next.next = new ListNode(1);
System.out.println("删除反复节点前的链表");
ListNode cur = pHead;
while (cur != null) {System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
System.out.println("删除反复节点后的链表");
Jz56 jz56 = new Jz56();
ListNode result = jz56.deleteDuplication(pHead);
cur = result;
while (cur != null) {System.out.print(cur.val + " ");
cur = cur.next;
}
}
}
【每日寄语】每个充满希望的凌晨,通知本人致力,是为了遇见更好的本人。