careercup-链表 2.3

2.3 实现一个算法,删除单向链表中间的某个结点,假设你只能访问该结点。(即你不知道头结点)

这个问题的关键是你只有一个指向要删除结点的指针,如果直接删除它,这条链表就断了。 但你又没办法得到该结点之前结点的指针,是的,它连头结点也不提供。在这种情况下, 你只能另觅他径。重新审视一下这个问题,我们只能获得从c结点开始后的指针, 如果让你删除c结点后的某个结点,那肯定是没问题的。比如删除结点d,那只需要把c 的next指针指向e,然后delete d就ok了。好的,如果我们就删除结点d,我们将得到 a->b->c->e,和目标链表只差了一个结点。怎么办呢?把d的数据给c! 结点结构都是一样的,删除谁都一样,最关键的是结点中的数据,只要我们留下的是数据 a->b->d->e就OK了

思路已经有了,直接写代码?等等,写代码之前,让我们再简单分析一 下可能出现的各种情况(当然包括边界情况)。如果c指向链表的:1.头结点;2.中间结点。 3.尾结点。4.为空。情况1,2属于正常情况,直接将d的数据给c,c的next指针指向d 的next指向所指结点,删除d就OK。情况4为空,直接返回。情况3比较特殊,如果c 指向尾结点,一般会认为直接删除c就ok了,反正c后面也没有结点了,不用担心链表断开。 可是真的是这样吗?代码告诉我们,直接删除c,指向c的那个结点(比如说b)的next指针 并不会为空。也就是说,如果你打印这个链表,它还是会打印出和原来链表长度一样的链表, 而且最后一个元素为0

关于这一点,原书CTCI中是这么讲的,这正是面试官希望你指出来的。然后, 你可以做一些特殊处理,比如当c是尾结点时,将它的数据设置为一些特殊字符, 这样在打印时就可以不打印它。当然也可以直接不处理这种情况,原书里的代码就是这么做 的。这里,也直接不处理这种情况。

  C++实现代码:

#include<iostream>
#include<new>
using namespace std;

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x):val(x),next(NULL) {}
};

void createList(ListNode *&L)
{
    int arr[10]= {1,2,3,2,5,6,7,3,9,1};
    int i;
    ListNode *p=NULL;
    for(i=0; i<10; i++)
    {
        ListNode *tmp=new ListNode(arr[i]);
        if(L==NULL)
        {
            L=tmp;
            p=tmp;
        }
        else
        {
            p->next=tmp;
            p=tmp;
        }
    }
}

void deleteNode(ListNode *c)
{
    if(c==NULL||c->next==NULL)
        return;
    ListNode *d=c->next;
    c->next=d->next;
    c->val=d->val;
    d->next=NULL;
    delete(d);
}

int main()
{
    ListNode *head=NULL;
    createList(head);
    ListNode *p=head;
    while(p)
    {
        cout<<p->val<<" ";
        p=p->next;
    }
    cout<<endl;
    deleteNode(head->next->next->next);
    p=head;
        while(p)
    {
        cout<<p->val<<" ";
        p=p->next;
    }
    cout<<endl;
}
时间: 2024-08-04 09:03:39

careercup-链表 2.3的相关文章

CareerCup之2.2 寻找单链表倒数第n个元素

[题目] 原文: 2.2 Implement an algorithm to find the nth to last element of a singly linked list. 译文: 实现一个算法从一个单链表中返回倒数第n个元素. [分析] (1)创建两个指针p1和p2,指向单链表的开始节点. (2)使p2移动n-1个位置,使之指向从头开始的第n个节点.(意思是就是使p1和p2距离n个位置) (3)接下来检查p2 - > = = null 如果yes返回p1的值,否则继续移动p1和 p

CareerCup之2.1无序链表删除重复元素

[题目] 原文: 2.1 Write code to remove duplicates from an unsorted linked list. FOLLOW UP How would you solve this problem if a temporary buffer is not allowed? 译文: 从一个未排序的链表中移除重复的项 进一步地, 如果不允许使用临时的缓存,你如何解决这个问题? [分析] (1)如果可以使用额外的存储空间,我们就开一个数组来保存一个元素的出现情况.

[CareerCup] 2.7 Palindrome Linked List 回文链表

2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome Linked List 回文链表.

[CareerCup] 2.6 Linked List Cycle 单链表中的环

2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.DEFINITIONCircular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the

[CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项

2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this problem if a temporary buffer is not allowed? 这道题让我们移除无序链表中的重复项,在LeetCode中有两道类似的题是Remove Duplicates from Sorted List 移除有序链表中的重复项 和 Remove Duplicates fr

[CareerCup] 2.2 Kth to Last Element of Linked List 链表的倒数第k个元素

2.2 Implement an algorithm to find the kth to last element of a singly linked list. 这道题让我们求链表中倒数第k个元素,LeetCode中相类似的题目有Kth Largest Element in an Array 数组中第k大的数字 和 Kth Smallest Element in a BST 二叉搜索树中的第K小的元素.但那两道题和这题又不一样,首先这道题是要在链表中操作,链表的特点就是不能通过下标来直接访

[CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点

2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.EXAMPLEInput: the node c from the linked list a->b->c->d->eResult: nothing is returned, but the new linked list looks like a- >

[CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表

4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D, you'll have D linked lists).

[LeetCode]2. Add Two Numbers用链表逆序存储的两个数相加

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST

引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to merge two binary search tree into balanced binary search tree.. Let there be m elements in first tree and n elements in the other tree. Your merge funct