![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3e68439c7f15501fb5e207f58261776e.png)
代码演示
package swordfingeroffer;
public class InterviewQuestion18 {
public static class ListNode{
int value;
ListNode next;
public ListNode(int value) {
this.value = value;
}
}
public ListNode deleteListNode(ListNode listHeader,ListNode deleteNode){
if (listHeader == null || deleteNode == null){
return null;
}
if (deleteNode.next != null){
ListNode tempListNode = deleteNode.next;
deleteNode.value = tempListNode.value;
deleteNode.next = tempListNode.next;
}else if (listHeader == deleteNode){
listHeader = null;
}else {
ListNode tempListNode = listHeader;
while (tempListNode.next != deleteNode){
tempListNode = tempListNode.next;
}
tempListNode.next = null;
}
return listHeader;
}
public void printList(ListNode listNode) throws Exception {
if (listNode == null){
throw new Exception("链表不能为空!");
}
while (listNode != null){
System.out.print(listNode.value + " ");
listNode = listNode.next;
}
}
public static void main(String[] args) {
InterviewQuestion18 interviewQuestion18 = new InterviewQuestion18();
ListNode listHeader = new ListNode(5);
ListNode listNode1 = new ListNode(9);
ListNode listNode2 = new ListNode(10);
ListNode listNode3 = new ListNode(15);
listHeader.next = listNode1;
listNode1.next = listNode2;
listNode2.next = listNode3;
listHeader = interviewQuestion18.deleteListNode(listHeader, listHeader);
try {
interviewQuestion18.printList(listHeader);
} catch (Exception e) {
e.printStackTrace();
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c342186c64849bd93f23702f0a7bc609.png)
代码演示:
package swordfingeroffer;
public class InterviewQuestion182 {
public static class ListNode{
int value;
ListNode next;
public ListNode(int value) {
this.value = value;
}
}
public ListNode deleteDuplication(ListNode pHead) {
if (pHead == null) {
throw new RuntimeException("链表不能为空!");
}
ListNode preNode = null;
ListNode pNode = pHead;
while (pNode != null) {
ListNode pNext = pNode.next;
boolean toDelete = false;
if (pNext != null && pNext.value == pNode.value) {
toDelete = true;
}
if (!toDelete) {
preNode = pNode;
pNode = pNext;
} else {
ListNode pToBeDelete = pNode;
int value = pNode.value;
while (pToBeDelete != null && pToBeDelete.value == value) {
pNext = pToBeDelete.next;
pToBeDelete = pNext;
}
if (preNode == null) {
pHead = pNext;
} else {
preNode.next = pNext;
}
pNode = pNext;
}
}
return pHead;
}
public void printList(ListNode pHead) {
if (pHead == null) {
throw new RuntimeException("打印链表不能为空!");
}
ListNode pNode = pHead;
while (pNode != null) {
System.out.print(pNode.value + " ");
pNode = pNode.next;
}
System.out.println();
}
public static void main(String[] args) {
InterviewQuestion182 interviewQuestion182 = new InterviewQuestion182();
ListNode pHead = new ListNode(5);
ListNode listNode1 = new ListNode(6);
ListNode listNode2 = new ListNode(6);
ListNode listNode3 = new ListNode(7);
pHead.next = listNode1;
listNode1.next = listNode2;
listNode2.next = listNode3;
pHead = interviewQuestion182.deleteDuplication(pHead);
interviewQuestion182.printList(pHead);
}
}