1.已知链表的头结点head,写一个函数把这个链表逆序
void List::reverse() { list_node * p = head; list_node * q = p->next; list_node * r = NULL; while(q){ r = q->next; q->next = p; p = q; q = r; } head->next = NULL; head = p; }
递归方法:
void List::reverse2(list_node * curnode) { if(curnode ==NULL)curnode = head; if(curnode->next==NULL) { cur = curnode; return; } reverse2(curnode->next); curnode->next->next=curnode; if(curnode == head) { head=cur; cur = curnode; cur->next = NULL; } }
2.已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。
void List::merge(List & list) { list_node * a = head; list_node * b = list.head; list_node * tempa= a; list_node * tempb = b; while(a&&b) { if(a->value <= b->value) { while(a&&b&&a->value <= b->value) { tempa = a; a = a->next; } tempa->next=b; } else { while(a&&b&&a->value > b->value) { tempb = b; b = b->next; } tempb->next=a; } } }
递归方法:
list_node* List::recursive_merge(list_node * a,list_node * b) { if(a == NULL)return b; if(b == NULL)return a; if(a->value <= b->value){ a->next=recursive_merge(a->next,b); return a; } if(a->value > b->value) { b->next=recursive_merge(a,b->next); return b; } }
3.有一个链表L,其每个节点有2个指针,一个指针next指向链表的下个节点,另一个random随机指向链表中的任一个节点,可能是自己或者为空,写一个程序,要求复制这个链表的结构并分析其复杂性
这个题目的方法很巧妙,将两个链表连接起来形成一个链表,设置好random指针后再将两个链表分割开来。
List List::copyRndList() { list_node * newhead = NULL; list_node * newcur = NULL; list_node * cur = head; while(cur) { if(newhead == NULL){ newhead = new list_node; newhead->value = cur->value; newhead->next = cur->next; cur->next = newhead; } else{ newcur = new list_node; newcur->value = cur->value; newcur->next = cur->next; cur->next = newcur; } cur=cur->next->next; } cur = head; while(cur){ if(cur->rnd) cur->next->rnd=cur->rnd->next; else cur->next->rnd = NULL; cur = cur->next->next; } cur = head; list_node * dst = cur->next; while(cur) { list_node * temp = dst->next; cur->next = temp; if(temp)dst->next = temp->next; cur = cur->next; dst=dst->next; } List newList; newList.head = newhead; return newList; }
4. 找出单向链表中中间结点
两个指针,一个步长为1,另一个步长为2.步长为2的走到底后步长为1的正好到中间。
list_node * List::middleElement() { list_node * p = head; list_node * q = head->next; while(q){ p = p->next; if(q)q=q->next; if(q)q=q->next; } }
5. 如何检查一个单向链表上是否有环
同样两个指针,一个步长为1,另一个步长为2,如果两个指针能相遇则有环。
list_node * List::getJoinPointer() { if(head == NULL || head->next == NULL)return NULL; list_node * one = head; list_node * two = head->next; while(one != two){ one = one->next; if(two)two=two->next; else break; if(two)two=two->next; else break; } if(one == NULL || two == NULL)return NULL; return one; }
6. 给定单链表(head),如果有环的话请返回从头结点进入环的第一个节点。
设链表头到环入口节点距离为x,环入口节点到两个指针相遇节点距离为z,换长度为y,则有x+z+1=y,所以z=y-1-x,即一个指针从链表头部开始移动,一个指针两个指针相遇后一个节点开始移动,相遇的地方即为环入口
list_node * List::findCycleEntry() { if(checkCycle()==false)return NULL; list_node * joinPointer = getJoinPointer(); list_node * p = head; list_node * q = joinPointer->next; while(p!=q) { p=p->next; q=q->next; } return p; }
7.只给定单链表中某个结点p(并非最后一个结点,即p->next!=NULL)指针,删除该结点。
将p后面那个节点的值复制到p,删除p后面的节点
void List::deleteByPointer(list_node * node) { if(node) { if(node->next){ node->value = node->next->value; node->next = node->next->next; } } }
8.在p前面插入一个节点
在p后面插入新节点,将p的值与新建的节点值互换。
9.给定单链表头结点,删除链表中倒数第k个结点
一个指针指向链表头,另一个指针指向第k个指针,然后两个指针一起移动,第二个指针到了末端则第一个指针就是倒数第k个节点
list_node * List::lastKelement(int k){ int t = k; list_node * p = head; while(p&&t){ p=p->next; t--; } if(p == NULL && t >0)return NULL; list_node * q=head; while(q && p){ p=p->next; q=q->next; } return q; }
10. 判断两个链表是否相交。
两种情况,如果链表有环,则先在环里设定一个指针不动,另一个链表从头开始移动,如果另一个链表能够与环中的指针相遇则是相交。
如果没有环,则判断两个链表的最后个节点是否相同,相同则相交
bool List::isIntersecting(const List & list) { bool flag = false; if(this->checkCycle()) { list_node * p = getJoinPointer(); list_node * q = list.head; while(q){ if(q == p){ flag = true; break; } q=q->next; } flag = true; } else { list_node * p = head; list_node * q = list.head; while(p->next)p=p->next; while(q->next)q=q->next; if(p == q)flag = true; else flag =false; } return flag; }
11. 两个链表相交,找出交点
求出两个链表的长度a和b,一个指针指向较短链表的头head,另一个指针指向较长链表的第head+|a-b|,然后两个指针一起移动,相遇处即为交点。
list_node * List::intersectNode(const List & list) { if(!isIntersecting(list))return NULL; int a = cnt; int b = list.cnt; list_node * p; list_node * q; if(a<b){p=list.head;q = head;} else {p = head; q=list.head;} a = abs(cnt - list.cnt); while(p && a) { p = p->next; a--; } while(p&&q) { if(q==p)break; p=p->next; q=q->next; } if(p && q && p == q)return p; return NULL; }