# 关于链表的总结（C++循环实现）

## 1.链表的基本操作

#### 1.1 结点定义

#include <iostream>

using namespace std;

struct Node
{
int value;
Node* next;
};


#### 1.2 创建链表

Node* createLinkedList(int data[], int len)
{
Node* ret = NULL;
Node* slider = NULL;

for(int i=0; i<len; i++)
{
Node* n = new Node();

n->value = data[i];
n->next = NULL;

if( slider == NULL )
{
slider = n;
ret = n;
}
else
{
slider->next = n;
slider = n;
}
}

return ret;
}


#### 1.3 销毁链表

void destroyLinkedList(Node* list)
{
while( list )
{
Node* del = list;

list = list->next;

delete del;
}
}


#### 1.4 打印链表

void printLinkedList(Node* list)
{
while( list )
{
cout << list->value << "->";

list = list->next;
}

cout << "NULL" << endl;
}


#### 1.5 获取链表长度

int getListLength(Node* list)
{
int ret = 0;

while( list )
{
ret++;
list = list->next;
}

return ret;
}


int main()
{
int a[] = {1, 5, 3, 2, 4};

cout << getListLength(list1) << endl;
cout << endl;

cout << getListLength(list2) << endl;
cout << endl;

int b[] = {6};
cout << getListLength(list3) << endl;

return 0;
}


1->5->3->2->4->NULL
5

NULL
0

6->NULL
1


## 2.结点的基本操作

#### 2.1 删除结点

Node* deleteNode(Node* list, int value)
{
Node* slider = NULL;

{
slider = NULL;
}

while( ret )
{
slider = ret->next;

if( slider && (slider->value == value) )
{
ret->next = slider->next;
slider = NULL;
}
else
{
ret = ret->next;
}
}

}


int main()
{
int a[] = {1, 2, 3, 2, 5};

cout << endl;

cout << endl;

int b[] = {2, 2, 2, 2, 2};
cout << endl;

int c[] = {1};

return 0;
}


1->2->3->2->5->NULL
1->3->5->NULL

NULL
NULL

2->2->2->2->2->NULL
NULL

1->NULL
1->NULL


#### 2.2 查找结点

Node* findNode(Node* list, int value)
{
Node* ret = NULL;
Node* slider = list;

while( slider )
{
if( slider->value == value )
{
ret = slider;
break;
}
else
{
slider = slider->next;
}
}

return ret;
}


## 3.面试题

#### 3.1 反转链表

Node* reverseLinkedList(Node* list)
{
Node* ret = NULL;
Node* slider = list;
Node* next = NULL;

while( slider )
{
next = slider->next;
slider->next = ret;
ret = slider;
slider = next;
}

return ret;
}


int main()
{
int a[] = {1, 5, 3, 2, 4};

cout << endl;

cout << endl;

int b[] = {6};

return 0;
}


1->5->3->2->4->NULL
4->2->3->5->1->NULL

NULL
NULL

6->NULL
6->NULL


#### 3.2 合并两个单向排序链表

Node* mergeLinkedList(Node* list1, Node* list2)
{
Node* ret = NULL;

if( list1 == NULL )
{
ret = list2;
}
else if( list2 == NULL )
{
ret = list1;
}
else
{
if( list1->value < list2->value )
{
ret = list1;
list1 = list1->next;
}
else
{
ret = list2;
list2 = list2->next;
}

Node* slider = ret;

while( list1 && list2 )
{
if( list1->value < list2->value )
{
slider->next = list1;
list1 = list1->next;
}
else
{
slider->next = list2;
list2 = list2->next;
}

slider = slider->next;
}

if( list1 == NULL )
{
slider->next = list2;
}
else if( list2 == NULL )
{
slider->next = list1;
}
}

return ret;
}


int main()
{
int a[] = {1, 2, 4, 6, 8};

int b[] = {2, 2, 3, 3, 7};

return 0;
}


1->2->4->6->8->NULL
2->2->3->3->7->NULL
1->2->2->2->3->3->4->6->7->8->NULL


#### 3.3 查找两个链表的第一个公共结点

Node* findFirstCommonNode(Node* list1, Node* list2)
{
int len1 = getListLength(list1);
int len2 = getListLength(list2);
Node* ret = NULL;

if( len1 > len2 )
{
for(int i=0; i<(len1-len2); i++)
{
list1 = list1->next;
}
}
else
{
for(int i=0; i<(len2-len1); i++)
{
list2 = list2->next;
}
}

while( list1 )
{
if( list1 == list2 )
{
ret = list1;
break;
}
else
{
list1 = list1->next;
list2 = list2->next;
}
}

return ret;
}


int main()
{
int a[] = {1, 2, 3};

int b[] = {4, 5};

int c[] = {6, 7};

Node* ret = NULL;

ret = list1;
while( ret->next )
{
ret = ret->next;
}
ret->next = list3;

ret = list2;
while( ret->next )
{
ret = ret->next;
}
ret->next = list3;

ret = findFirstCommonNode(list1, list2);

return 0;
}


1->2->3->6->7->NULL
4->5->6->7->NULL
6->7->NULL


#### 3.4 删除排序链表中重复的结点

Node* deleteDuplicationNode(Node* list)
{
Node* toDel = NULL;
Node* end = NULL;

// 处理头结点重复的情况
{
// 找到最后一个与头结点重复的结点
while( end && end->next && (end->value == end->next->value) )
{
end = end->next;
}
// 删除中间与头结点重复的结点
{
toDel = NULL;
}
// 删除最后一个与头结点重复的结点
toDel = NULL;
}

Node* slider = NULL;

while( ret && ret->next )
{
slider = ret->next;

// 处理中间结点重复的情况
while( slider && slider->next && (slider->value == slider->next->value) )
{
end = slider;
// 找到最后一个与中间结点重复的结点
while( end && end->next && (end->value == end->next->value) )
{
end = end->next;
}
// 删除中间与中间结点重复的结点
while( slider != end )
{
toDel = slider;
slider = slider->next;
toDel = NULL;
}
// 删除最后一个与中间结点重复的结点
toDel = slider;
slider = slider->next;
ret->next = slider;
toDel = NULL;
}

if( ret->next == slider )
{
ret = ret->next;
}
else
{
ret->next = slider;
}
}

}


int main()
{
int a[] = {1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5};

cout << endl;

cout << endl;

int b[] = {2, 3, 3, 3, 4, 4, 5, 6, 6, 7};
cout << endl;

int c[] = {1};

return 0;
}


1->1->1->2->2->2->3->4->4->4->5->5->NULL
3->NULL

NULL
NULL

2->3->3->3->4->4->5->6->6->7->NULL
2->5->7->NULL

1->NULL
1->NULL

posted @ 2018-12-25 15:34  PyLearn  阅读(324)  评论(0编辑  收藏  举报