链表

来源:互联网 发布:手机游戏网络修改器 编辑:程序博客网 时间:2024/05/21 14:00

定义节点:

typedef struct ListNode{      int        m_nKey;      ListNode*  m_pNext;}ListNode;


1.链表反转

ListNode* Reverse(ListNode* pHead){ListNode* curNode = pHead;ListNode* nextNode = pHead->m_pNext;ListNode* endNode = NULL;if (pHead == NULL || pHead ->m_pNext == NULL){return pHead;}curNode->m_pNext = NULL;//this is new tail node ,m_pNext pointer must be NULLwhile (nextNode){endNode = nextNode->m_pNext;nextNode->m_pNext = curNode;curNode = nextNode;nextNode = endNode;}return curNode;//new head}


2.查找链表中倒数第k个结点

ListNode * ListLastK(ListNode* head,unsigned int K){ListNode *slow = NULL;ListNode *fast = NULL;if (K<1 || head == NULL){return NULL;}while (K-- && fast != NULL){fast = fast->m_pNext;}if (K > 0){return NULL;//list len < K}while (fast != NULL){fast = fast->m_pNext;slow = slow->m_pNext;}return slow;}



3.取链表中位节点

(1)如果是偶数个节点,取前中位节点

ListNode * ListMid(ListNode* head){ListNode* fastNode = head;ListNode* slowNode = head;while (fastNode != NULL && fastNode->m_pNext != NULL){slowNode = slowNode->m_pNext;fastNode = fastNode->m_pNext->m_pNext}return slowNode;//return pre-Mid node}


(2)如果是偶数个节点,取后中位节点

ListNode *ListMid(ListNode* head){ListNode* fastNode = head;ListNode* slowNode = head;while (fastNode != NULL && fastNode->m_pNext != NULL){slowNode = slowNode->m_pNext;fastNode = fastNode->m_pNext->m_pNext}if (head != NULL && fastNode = NULL){slowNode = slowNode->m_pNext;}return slowNode;//return atfer-Mid node}


4.两个单向链表,有可能交叉,判断是否交叉,如果交叉,返回第一个交叉点,否则返回NULL

ListNode* intercross(ListNode* firstList,ListNode* secondList){ListNode *tempNode = NULL;ListNode *firstNode = firstList;ListNode *secondeNode = secondList;unsigned int listLen1 = 0;unsigned int listLen2 = 0;int decLen = 0;if (firstList == NULL && secondList == NULL){return NULL;}tempNode = firstList;while (tempNode != NULL){++listLen1;tempNode->m_pNext;}tempNode = secondList;while(tempNode != NULL){++listLen2;tempNode->m_pNext;}decLen = listLen1-listLen2;if (decLen>=0){if (decLen-- > 0){firstNode = firstNode->m_pNext;}}else{if (decLen++ <0){secondeNode = secondeNode->m_pNext;}}while (firstNode != NULL){if (firstNode == secondeNode){return firstNode;}}return NULL//does not find the same node ,so return NULL}



5.判断链表是否有环,如果有,返回进入环的第一个节点

ListNode * Listloop(ListNode* head){char isLoop = 0;ListNode* slow = NULL;ListNode* fast = NULL;ListNode* newHead = NULL;ListNode* crossNode = NULL;slow = fast = head;while (fast != NULL && fast->m_pNext != NULL){fast = fast->m_pNext->m_pNext;slow = slow->m_pNext;if (fast == slow){isLoop = 1;break;//this is loop list}}//here we can see the list is go end then is not loop or is loop because isloop is 1if (isLoop == 1){//find the entry node//break the loop,then we have two list ,find the intercross node ,that's what we want wantnewHead = slow->m_pNext;slow->m_pNext = NULL;//break the loopcrossNode = intercross(head,newHead);slow->m_pNext = newHead;//connect the two list ,make it loop like beforereturn crossNode;}else{return NULL;}}


原创粉丝点击