leetCode#83. Remove Duplicates from Sorted List

Description

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

Code

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head == None:
            return head
        pre = p = head
        val = head.val
        while p != None:
            if val != p.val:
                pre.next, val, pre = p, p.val, p
            p = p.next
        pre.next = None
        return head

自己写的传统解法

wen587sort的递归解法,这个没想到,学习学习。

public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null)return head;
        head.next = deleteDuplicates(head.next);
        return head.val == head.next.val ? head.next : head;
}

不过,往下的评论似乎是对于这种链表的题用递归来解决有些微词。

I highly doubt if we should use recursion in solving linked list problems. We use it for tree because its stack space is O(logn), where n is the number of nodes. But it’s O(n) space required for linked list, which is very likely to be stack overflow. Point me out if you hold a different opinion.

@lisen I have the same question. But I don’t understand what do you mean stack overflow in this case?

What they mean is that every time we make a recursive call, it places a frame on our stack memory. Because we much make a recursive call for each node in list, recursion is often not the best way to manipulate lists if it can be avoided.

I see, thanks for your reply.
Since that recursive might cause stack overflow, why do people like to use recursive a lot? There are many concise codes that are written in recursive.
Does recursive used a lot in the real industry? Sorry, I have no much working experience.

Well, on sites like these one’s, I believe a lot of times people want to post ‘other ways’ of doing things, which is good. Recursive solutions sometimes require less coding to implement, and as a by-product of this look more clever or elegant. Recursion is just a tool. Sometimes the job calls for a hammer. Other times, a hammer is not suitable. Part of being a good programmer is knowing when to use which tools. But on sites like these, some people like to ‘sharpen their tools’ by writing recursive solutions to problems where iterative solutions are more efficient. Think of it as practice.

总的来说,意思就是,关于链表,用递归来解决的话,有炫技的意思,并不是很使用。实际生产中,用递归也可能造成栈溢出的问题。好的程序员应该懂得选择合适的方法去解决问题,而不是为了代码的精简或者优雅,去写尽可能短的代码。

Conclusion

题目是个简单的题目,但是学习到了另一种解题思路,而且学习到了关于递归的更深层的理解。平常用递归的时候,总是想到递归精简,逻辑简单,但是一直忽略了递归造成的内存开销问题。这题很好的提醒了我。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值