剑指offer18--删除链表的节点——java/C++实现

在O(1)时间内删除链表节点

给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点。

思路:

  1. 空节点
  2. 要删除的节点不是尾节点
  3. 链表只有一个节点,删除头结点
  4. 链表中有多个节点,删除尾节点

 

删除链表中重复的结点

题目描述

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表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;
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值