面试题整理13 合并排序链表去重
生活随笔
收集整理的這篇文章主要介紹了
面试题整理13 合并排序链表去重
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:合并兩個排序鏈表,去掉重復元素
struct ListNode {int m_nValue;ListNode* m_pNext; }; using namespace std;ListNode* MergeLists(ListNode* pHead1,ListNode* pHead2) {if(pHead1 == NULL && pHead2 == NULL)return NULL;if(pHead1 == NULL && pHead2 != NULL)return pHead2;if(pHead1 != NULL && pHead2 == NULL)return pHead1;ListNode* newListHead = pHead1;if( pHead1->m_nValue > pHead2->m_nValue ){swap(pHead1,pHead2);}while( pHead1!= NULL && pHead2 != NULL){while( pHead1 != NULL && pHead1->m_pNext != NULL && pHead1->m_pNext->m_nValue < pHead2->m_nValue){//qu chong while( pHead1->m_pNext != NULL && pHead1->m_nValue == pHead1->m_pNext->m_nValue){pHead1->m_pNext = pHead1->m_pNext->m_pNext;}if( pHead1->m_pNext->m_nValue < pHead2->m_nValue){pHead1 = pHead1->m_pNext;}}if( pHead1!=NULL && pHead1->m_nValue == pHead2->m_nValue){while( pHead2 != NULL && pHead2->m_nValue == pHead1->m_nValue ){pHead2 = pHead2->m_pNext;}}ListNode* tempNode = pHead1->m_pNext;pHead1->m_pNext = pHead2;pHead1 = pHead2;pHead2 = tempNode;}return newListHead; }總結
以上是生活随笔為你收集整理的面试题整理13 合并排序链表去重的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 面试题整理12 求字符串括号最大深度子串
- 下一篇: trie数 字典树