C语言链表反转

 面试经典题目之一

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

/**
 * 
 * @param pHead ListNode类 
 * @return ListNode类
 */
struct ListNode* ReverseList(struct ListNode* pHead ) {
    // write code here

    struct ListNode*p=pHead;
    if(p==NULL || p->next==NULL)
    {
        return p;
    }
    struct ListNode *pre=NULL;
    struct ListNode *next=NULL;
    while(p!=NULL)
    {
        next = p->next;
        p->next=pre;
        pre=p;
        p=next;
    }
    return pre;
}

牛客网在线OJ结果

C语言链表反转_第1张图片

你可能感兴趣的:(链表,c语言,面试)