C语言题库(3)

1.反转链表

描述:将链表(头结点要存放数据)反转,如输入的是{1,2,3,4},反转之后得到{4,3,2,1}


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

struct ListNode* ReverseList(struct ListNode* pHead ) {
    // write code here
    struct ListNode *p=NULL;
    struct ListNode *temp=NULL;
    while(pHead!=NULL){
        temp=pHead->next;
        pHead->next =p;
        p=pHead;
        pHead = temp;   
    }
    pHead = p;
    return pHead;
}

猜你喜欢

转载自blog.csdn.net/NXBBC/article/details/126373532