You are here:

C++/split a linked list into two linked list


Thanks Eddie,

I inserted the function and with some modificatiion for the purpose it did work.

I am coverting this to a class template but it does not work. This time i am passing in a second list and an item.

below is my code

template <class Type>
void splitAt(List <Type> &seclist, const Type &item){
    Node <Type> * npHead;
    Node <Type> * npTail;
    Node <Type> *pNode = npHead;
    bool found = false;

    if (isEmpty())
       cout<< "The List is empty\n";
        for (pNode = pHead; pNode != NULL; pNode = pNode->pNext)
         if (item == pNode->nData){
         npHead = pNode;

         seclist = npHead;
         found = true;
         if (found = false){cout<<"Item not found";}

Thanks for any help.

Followup To
Question -
I am new to C++ and would like if you can help me.

I am trying to split a linked list into another two linked list and print out the results.

I have datas 2 3 4 5 6 7 8 9

i would like it to split in value 6

my first list will display 2 3 4 5
second list 6 7 8 9

How to i get a linked lits to split

Thanks in advance for you help

Answer -

This is a little tricky. First off, you would need to be keeping some kind of global/member variable to store the head of the new list. That said, I'm going to try and write a split list function to see if it helps you understand more:

// List.cpp globals
Node *pNewHead;

void List::SplitList(const int iSplitValue)
Node *pCurr = NULL;

for(pCurr = pHead; pCurr; pCurr = pCurr->pNext)
if(pCurr->pNext->iValue == iSplitValue)
pNewHead = pCurr->pNext;
pCurr->pNext = NULL;

It may appear a little confusing, but what I did above was iterated until the current node's next pointer was equal to the value to split on, that was you can detach that node from the first list by setting the current node's next = to NULL. Before you do that you set the new head to be the current node's next so you have a pointer to the front of the new list, and the new list is still intact, since we never touched any of the connecting next pointers when we split from the old list.

I hope this information was helpful.

- Eddie


I see a couple issues with your code logic up there. The first being that you are setting an entire list to a single node pointer. The second lies within that, you are setting a reference equal to a pointer, so I don't believe that will work. Also, try putting a "break" right after setting found to true. One more thing is that it doesn't appear that you are completely detaching the old list from the new list.

I hope this information was helpful.

- Eddie  


All Answers

Answers by Expert:

Ask Experts




I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by I also know a good deal about graphics via OpenGL, and GUIs.


I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 All rights reserved.