关闭

Leetcode 237. Delete Node in a Linked List

73人阅读 评论(0) 收藏 举报

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

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

本题只给出需要删除的节点,前置节点,链表头节点都没有给出,所以无法直接删除。只能从删除结点开始,将后面节点的值依次向前挪。

我的解法:

遇到问题:while的终止条件需提前一个节点,否则无法删除最后一个节点。之前写的while(after != null)

public class Solution {
    public void deleteNode(ListNode node) {
        ListNode now = node;
        ListNode after = node.next;
        
        while(after.next != null){
            now.val = after.val;
            now = now.next;
            after = after.next;
        }
        
        now.val = after.val;
        now.next = null;
        
    }
}



discuss里的简单解法:

问题:没有判定条件,循环不会一直执行吗?

public void deleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
}


0
0
查看评论

LeetCode 237 : Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -&...
  • sunao2002002
  • sunao2002002
  • 2015-07-27 13:54
  • 5160

LeetCode(237)Delete Node in a Linked List

题目Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -&...
  • fly_yr
  • fly_yr
  • 2015-08-15 21:32
  • 1040

Leetcode_237_Delete Node in a Linked List

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47334649 Write a function to delete a node (except the tail) in a singly link...
  • pistolove
  • pistolove
  • 2015-08-07 09:53
  • 1285

237. Delete Node in a Linked List [easy] (Python)

题目链接https://leetcode.com/problems/delete-node-in-a-linked-list/题目原文 Write a function to delete a node (except the tail) in a singly linked list, giv...
  • coder_orz
  • coder_orz
  • 2016-05-13 21:18
  • 1391

[leetcode-237]Delete Node in a Linked List(C语言)

问题描述: Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 ->...
  • zdavb
  • zdavb
  • 2015-07-17 10:55
  • 703

leetcode 237: Delete Node in a Linked List

leetcode 237: Delete Node in a Linked List PYTHON JAVA C++
  • xudli
  • xudli
  • 2015-07-15 14:50
  • 16990

[leetcode]237. Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -&g...
  • u013805817
  • u013805817
  • 2017-01-18 14:54
  • 135

237. Delete Node in a Linked List LeetCode

题意:删除链表的给定节点。 题解:比如a->b->c->d->e,我们要删除c。那么我把d的值赋给c,把c的next链接到e,然后把d删除,就完成了删除c。class Solution { public: void deleteNode(ListNode* node...
  • youthinkwu
  • youthinkwu
  • 2016-02-10 12:11
  • 100

LeetCode *** 237. Delete Node in a Linked List

题目: Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -&g...
  • treeshy
  • treeshy
  • 2016-04-06 15:55
  • 63

[LeetCode] 237. Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 ->...
  • sai_j
  • sai_j
  • 2017-07-11 11:54
  • 56
    个人资料
    • 访问:821次
    • 积分:104
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章存档