简体   繁体   中英

Sorting a linked list

I am having an issue of a Null pointer exception. As much as i try i can't find any sort of help. If someone has an idea please let me know.

for (cursor = head; cursor != null; cursor = cursor.link) {

    k = addScore(cursor.num);
    for (int i = 1; i <= nodeLength(); i++) {

        cursorAdd = head.link;
        j = addScore(cursorAdd.num);

        if (j > k) {

            cursor.link = cursorAdd.link;
            cursorAdd.link = cursor;
        }
        cursorAdd = cursorAdd.link;
    }
}

在使用之前,不要检查cursorAdd != null

I think your list has only one element. So

head != null
head.link == null;
cursor = head; // cursor != null; cursor.link == null.
cursorAdd = cursor.link; // == null
addScore(cursorAdd.num) <-- NPE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM