Skip to content

Commit bfe84db

Browse files
committed
Update Partition_List.cc
Add a version.
1 parent 46ee88e commit bfe84db

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Partition_List.cc

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,29 @@ class Solution {
3535
}
3636
return head;
3737
}
38-
};
38+
};
39+
40+
//Another method using second level pointer
41+
class Solution {
42+
public:
43+
ListNode *partition(ListNode *head, int x) {
44+
// Start typing your C/C++ solution below
45+
// DO NOT write int main() function
46+
ListNode *ret = NULL;
47+
ListNode **tail = &ret;
48+
ListNode **pHead = &head;
49+
ListNode *entry = NULL;
50+
for (ListNode **pCur = &head; *pCur; ) {
51+
entry = *pCur;
52+
if (entry->val < x) {
53+
*pCur = entry->next;
54+
*tail = entry;
55+
tail = &(entry->next);
56+
} else {
57+
pCur = &(entry->next);
58+
}
59+
}
60+
*tail = *pHead;
61+
return ret;
62+
}
63+
};

0 commit comments

Comments
 (0)