您的位置:首页 > Web前端 > Node.js

237. Delete Node in a Linked List

2017-09-13 16:52 295 查看
Writea function to delete a node (except the tail) in a singly linked list, givenonly access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given thethird node with value 3, the linked list should become 1 -> 2 -> 4 aftercalling your function.

 

 

这个问题要删除一个结点,不过和普通有点不同,不知道头结点,知道的是指向要删除的结点的指针,

解决方案是,把下一个结点的值覆盖了这个结点,然后把下一个结点删除.代码如下:

 

/**

 * Definition forsingly-linked list.

 * struct ListNode {

 *     intval;

 *    struct ListNode *next;

 * };

 */

void deleteNode(structListNode* node) {

    struct ListNode*p = node->next;

    node ->val=p->val;

    node ->next =p->next;

    free(p);

}

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: