# Cracking the Coding Interview（linked list）

class  LinkNode
{
public:
int isvisit;
protected:
private:
};



LinkNode* createlinkedlist()
{
}
{
}
{
while (iterator->next != NULL)
{
iterator = iterator->next;
}
ln->next = iterator->next;
iterator->next = ln;
}
{
while (iterator->next != NULL)
{
iterator->next = iterator->next->next;
iterator = iterator->next;
}
}


LinkNode* addloop (LinkNode* ln,LinkNode* head)
{
while (iterator->next != ln)
{
iterator = iterator->next;
}
while (iterator->next != NULL)
{
iterator = iterator->next;
}
iterator->next = newiterator;
}


1.Wirte code to remove duplicates from an unsorted linked list。How would you solve this problem if a temporary buffer is not allowed？

（1）

LinkNode* removeduplicate1(LinkNode* head)
{
int array[26] = {0};
while (iterator->next != NULL)
{
iterator->next = iterator->next->next;
else
iterator = iterator->next;
}
}


（2）

LinkNode* removeduplicate2(LinkNode* head)
{
{
}
}


书中的一些解决方案：

2.Implement an algorithm to find the nth to last element of a single linked list.

LinkNode* findlastnthnode(LinkNode* head,int n)
{
int count = 0;
while(iterator != NULL)
{
iterator = iterator->next;
if (count >= n)
{
niterator = niterator -> next;
}
count++;
}
return niterator;
}


书中的一些解决方案：

3.Implement an algorithm to delete a node in the middle of a single linked list given only access to that node.For example:

Input:the node 'c' from the linked list a->b->c->d->e

Result:nothing is returned,but the new linked list looks like a->b->d->e

LinkNode* deletemiddle(LinkNode* head,int linknum)
{
int prevcount = 0;
int nextcount = 0;
{
prevcount++;
{
nextcount = 0;
while (newiterator->next != NULL)
{
nextcount++;
newiterator = newiterator->next;
}
if (prevcount == nextcount)
{
iterator->next = iterator->next->next;
break;
}
}
iterator = iterator->next;
}
}


书中的一些解决方案：

我不太确定书中的描述：

The Solution to this is to simply copy the data from the next node into this node and then delete the next node.

NOTE:This problem can not be solved if the node to be deleted is the last node in the linked list.???这里的描述不是很明白。

4.You have two numbers represented by a linked list,where each node contains a single digit.The digits are stored in reverse order,such that the 1's digit is at the head of the list.Write a function that adds the two numbers and returns the sum as a linked list.For example:

input:3->1->5+5->9->2

output:8->0->8

LinkNode* add(LinkNode* h1,LinkNode* h2)
{
int back = 0;
while(it1 != NULL || it2 != NULL)
{
int num = back;
back = 0;
if (it1 != NULL)
{
it1 = it1->next;
}
if (it2 != NULL)
{
it2 = it2->next;
}
if (num >= 10)
{
num -= 10;
back ++;
}
}
if (back != 0)
{
}
}


书中的一些解决方案：

整体思路类似，只是书中的参考利用递归计算。

5.Given a circular linked list,implement an algorithm which returns node at the beginning of the loop.

Definition:

Circular linked list:A linked list in which a node's next pointer points to an eariler node,so as to make a loop in the linked list.

For example:

input:a->b->c->d->e->c

output:c

这个题目看起来十分容易，但是我写起来的时候却想不起来解决方案。因为遍历这个链表根本不会有结束的条件，死循环。我执行程序的时候发现的这个问题。

//broken the linkedlist node,maybe I can use an array or vector?
{
while (iterator->isvisit == 0)
{
iterator->isvisit = 1;
iterator = iterator->next;
}
return iterator;
}


书中的解决方案：

这个题目的思考方式我觉得挺新颖的。可以这么理解，比如一个操场上两个人从共同的起点一起跑步，一个人的速度刚好是另外一个人速度的两倍，所以他们第二次必定相遇在起点的地方。这样就存在了一个结束条件。

LinkedListNode FindBeginning(LinkedListNode head) {
//step1 meeting before k at beginning loop
while(n2.next != null) {
n1 = n1.next;
n2 = n2.next.next;
if ( n1 == n2)
break;
}
if(n2.next == null) {
return null;
}