js
this.val = x;
this.next = null;
}*/
function ReverseList(pHead)
{
// write code here
if(!pHead || !pHead.next){
return pHead
}
let pre = null
let cur = null
while(pHead){
cur = pHead.next
pHead.next = pre
pre = pHead
pHead = cur
}
return pre
}
module.exports = {
ReverseList : ReverseList
};
C语言
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
*
* @param pHead ListNode类
* @return ListNode类
*/
struct ListNode* ReverseList(struct ListNode* pHead ) {
// write code here
if(pHead == NULL)
return NULL;
struct ListNode*temp = NULL;
struct ListNode*pre = NULL;
while(pHead != NULL){
temp = pHead->next;
pHead -> next = pre;
pre = pHead;
pHead = temp;
}
return pre;
}