在O(1)时间内删除链表节点
给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点。
思路:
- 空节点
- 要删除的节点不是尾节点
- 链表只有一个节点,删除头结点
- 链表中有多个节点,删除尾节点
删除链表中重复的结点
题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
思路:
一定要定义一个新的头指针,一共3个指针,还得有一个临时指针专门用来删除
然后就是不要老想着return,多利用循环条件。把null放在尾部也是可以返回的。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead)
{
if (pHead == NULL || pHead->next == NULL)
return pHead;
ListNode* pFront=new ListNode(-1);
ListNode* pMiddle=pHead;
ListNode* pNext=pMiddle->next;
while (pMiddle!=NULL)
{
pNext=pMiddle->next;
bool flag=false;
if (pMiddle->val==pNext->val&&pNext!=NULL)
flag=true;
if (!flag)
{
pFront=pMiddle;
pMiddle=pNext;
}
else
{
ListNode* pDel=pMiddle;
int value=pDel->val;
while(pDel!=NULL&&pDel->val==value)
{
pMiddle=pMiddle->next;
delete pDel;//VS下不能按照剑指offer的delete
pDel=NULL;
pDel=pMiddle;
}
if(pFront->val==-1)
pHead=pMiddle;
else
pFront->next=pMiddle;
}
}
return pHead;
}
};
调试代码:
按照剑指offer做的。
#include <iostream>
using namespace std;
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
ListNode* deleteDuplication(ListNode* pHead)
{
if (pHead == NULL || pHead->next == NULL)
return pHead;
ListNode* pFront = new ListNode(-1);
ListNode* pMiddle=pHead;
ListNode* pNext = pMiddle->next;
while (pMiddle!=NULL)
{
bool flag=false;
pNext = pMiddle->next;
if(pNext->val==pMiddle->val&&pNext!=NULL)
//如果pNext==NULL,那么就直接往后挪,依靠循环条件把null放进来
flag=true;
if (!flag)
{
pFront=pMiddle;
pMiddle=pMiddle->next;
}else{
int value=pMiddle->val;
ListNode* DelPtr=pMiddle;
while (DelPtr!=NULL&&DelPtr->val==value)
{
pNext=DelPtr->next;
DelPtr=pNext;
}
if (pFront->val==-1)//第一次删除
{
pHead=pNext;
}
else
{
pFront->next=pNext;//跳过删除的节点,这里next有可能是NULL
}
pMiddle=pNext;//这里next有可能是NULL
}
}
return pHead;
}
int main()
{
ListNode first(0);
/*ListNode second(2);
ListNode thrid(3);
ListNode fourth(3);
ListNode fifth(4);
ListNode sixth(4);
ListNode seventh(5);*/
ListNode second(1);
ListNode thrid(1);
ListNode fourth(1);
ListNode fifth(1);
ListNode sixth(1);
ListNode seventh(1);
ListNode* head = &first;
head->next = &second;
head->next->next = &thrid;
head->next->next->next = &fourth;
head->next->next->next->next = &fifth;
head->next->next->next->next->next = &sixth;
head->next->next->next->next->next->next = &seventh;
deleteDuplication(head);
return 0;
}
Java实现
删除单个节点:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode tmp;
ListNode res = head;
ListNode front = head;
while(head.val != val){
front = head;
head = head.next;
if (head == null){
break;
}
}
if(front == head){
return head.next;
}else{
front.next = head.next;
}
return res;
}
}
删除重复节点
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode deleteDuplication(ListNode pHead)
{
if(pHead == null || pHead.next == null){
return pHead;
}
ListNode res = pHead;
ListNode front = pHead;
ListNode after = pHead.next;
while(after != null){
if(after.val != pHead.val){
front = pHead;
pHead = after;
after = after.next;
continue;
}
while(after != null && after.val == pHead.val){
after = after.next;
}
if(front.val == pHead.val){
res = after;
front = after;
}else{
front.next = after;
}
if(after != null){
pHead = after;
after = after.next;
}
}
return res;
}
}