- Remove Linked List Elements
这两个都是easy题,还是写错:(
当首节点也要处理的时候,最简单就是创建一个新的dummy节点。
两种方法:
一种是alloc (new)一个node,function ends时需要free。
一种是创建一个local node,function ends时memory自动回收。
这里涉及到最基本的stack vs heap:
Differences between Stack and Heap
Stack and a Heap ?
http://net-informations.com/faq/net/stack-heap.htm
网上随便搜的一个,但是我觉得讲的挺好的。
Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer’s RAM .
1,stack
when allocated?
Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it’s allocation is dealt with when the program is compiled.
when freed?
When a function or a method calls another function which in turns calls another function etc., the execution of all those functions remains suspended until the very last function returns its value.
The stack is always reserved in a LIFO order, the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack, freeing a block from the stack is nothing more than adjusting one pointer.
2,heap
when allocated?
Variables allocated on the heap have their memory allocated at run time and accessing this memory is a bit slower, but the heap size is only limited by the size of virtual memory
when freed?
Element of the heap have no dependencies with each other and can always be accessed randomly at any time.
You can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time.
when to use?
You can use the stack if you know exactly how much data you need to allocate before compile time and it is not too big.
You can use heap if you don’t know exactly how much data you will need at runtime or if you need to allocate a lot of data.
这一段在multi-thread里特别重要,记得刚工作的时候写一个信号处理的算法,随意的在一个function里面创建了一个local variable, 1024bytes,然后直接cause exception error,当时一个thread的stack才有2K貌似。
In a multi-threaded situation each thread will have its own completely independent stack but they will share the heap. Stack is thread specific and Heap is application specific. The stack is important to consider in exception handling and thread executions.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummy = new ListNode(0);
ListNode* cur = dummy;
//ListNode dummy = ListNode(0);
//ListNode* cur = &dummy;
cur->next = head;
while(cur->next)
{
if(cur->next->val == val)
{
cur->next = cur->next->next;
//这时cur->next已经指向下下一个节点了,cur就不要move了,cur如果move的话,就会漏掉一个节点,因为loop的条件是cur->next
//ll写的时候最好画图
}
else
{
cur = cur->next;
}
}
ListNode* res = dummy->next;
free(dummy);
return res;
//return dummy.next;
}
};
- Delete Node in a Linked List
这题有个朋友面试fw的时候遇到过,就是用一行code delete a node,当时还是google出来的。
*node = *node->next;
如果不考虑tail,不考虑memory leak的话。
但是这个API到底什么时候才会被用的呢?
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
ListNode* next = node->next;
if(next == NULL)
{
*node = NULL;
}
else
{
*node = *node->next;
free(next);
}
}
};