0、链表的实现
class List {
public:
ListNode *root;
List()
{
root = new ListNode;
}
~List(){
delete root;
}
ListNode *create()
{
ListNode *p1,*p2 = nullptr;
ListNode *head=NULL;
cout<<"输入数序(以负数或0结束): ";
int n;
cin>>n;
while(n>0)
{
p1=new ListNode;
p1->data=n;
p1->next=NULL;
if(NULL==head){
head=p1;
p2=head;
}else{
p2->next=p1;
p2=p1;
}
cin>>n;
}
root = head;
return root;
}
ListNode *insert(ListNode *head, int p, int x)
{
cout<<"在第"<<p<<"元素后插入"<<x<<endl;
ListNode *tmp=head;
for(int i=0; i<p;i++)
{
if(tmp==NULL){
return head;
}
if(i<p-1){
tmp=tmp->next;
}
}
ListNode *tmp2=new ListNode;
tmp2->data=x;
tmp2->next=tmp->next;
tmp->next=tmp2;
return head;
}
ListNode* deleteNode(ListNode* head, int data)
{
ListNode* point = head;
if(point->data == data)
{
head = head->next;
free(point);
return head;
}
while(point->next != NULL)
{
if(point->next->data == data)
{
point->next = point->next->next;
return head;
}
point = point->next;
}
return head;
}
int del(ListNode *head,int pos)
{
ListNode *tmp = head;
for(int i=1;i<pos;i++)
{
if(tmp==NULL) return -1;
if(i<pos-1){
tmp=tmp->next;
}
}
int ret=tmp->data;
return ret;
}
int len(){
return len(root);
}
int len(ListNode *h)
{
int i=0;
while(h!=NULL)
{
i++;
h=h->next;
}
return i;
}
bool checkCircle(ListNode *head)
{
if (head == NULL) return false;
ListNode *p = head;
ListNode *q = head->next;
while ( q!=NULL) {
if (q == p) return true;
p = p->next;
if (q->next == NULL) return false;
else q = q->next->next;
}
return false;
}
void display(){
if (!checkCircle(root)) {
display(root);
}else{
cout<<"The circle Link!"<<endl;
}
}
void display(ListNode *head)
{
for(ListNode*tmp=head;tmp!=NULL;tmp=tmp->next){
printf("%d->",tmp->data);
}
printf("\n");
}
ListNode* reverseList(){
return reverseList(root);
}
ListNode* reverseList(ListNode* root) {
stack<int> stk;
ListNode *p = root;
while (p)
{
stk.push(p->data);
p = p->next;
}
p = root;
while (p)
{
p->data = stk.top();
stk.pop();
p = p->next;
}
return p;
}
ListNode* reverseList3(ListNode* pHead) {
if(pHead==nullptr)
return pHead;
ListNode* temp=nullptr;
ListNode* cur=pHead;
ListNode* node=nullptr;
while(cur!=nullptr)
{
node=cur->next;
cur->next=temp;
temp=cur;
cur=node;
}
root = temp;
return temp;
}
ListNode* deleteDuplicates(ListNode* head)
{
ListNode* point = head;
while(point != NULL && point->next!=NULL && point->data == point->next->data)
{
head = head->next;
root = head;
point = head;
}
while(point != NULL && point->next != NULL)
{
if(point->next->data == point->data)
{
point->next = point->next->next;
}else{
point = point->next;
}
}
return head;
}
};
1、链表反转
ListNode* reverseList(ListNode* pHead) {
stack<int> stk;
ListNode *p = pHead;
while (p)
{
stk.push(p->data);
p = p->next;
}
p = pHead;
while (p.root)
{
p->data = stk.top();
stk.pop();
p = p->next;
}
return p;
}
ListNode* reverseList3(ListNode* pHead) {
if(pHead==nullptr)
return pHead;
ListNode* temp=nullptr;
ListNode* cur=pHead;
ListNode* node=nullptr;
while(cur!=nullptr)
{
node=cur->next;
cur->next=temp;
temp=cur;
cur=node;
}
root = temp;
return temp;
}
2、合并两个有序的链表(递归)
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
if (list2==NULL)
{
return list1;
}
if (list1==NULL)
{
return list2;
}
if (list1->val < list2->val)
{
list1->next = mergeTwoLists(list1->next, list2);
return list1;
}else
{
list2->next = mergeTwoLists(list1, list2->next);
return list2;
}
}
验证
int main(int argc, const char * argv[]) {
Link link;
link.create();
link.display();
link.insert(link.root, 2, 3);
link.display();
Node *mid = link.findMidElement(link.root);
mid->next = link.root;
link.display();
link.reverseList();
link.display();
return 0;
}