当前位置:网站首页>《剑指Offer》 链表反转

《剑指Offer》 链表反转

2022-07-27 14:15:00 傻子是小傲娇

题目描述

输入一个链表,反转链表后,输出新链表的表头。

头插法: 

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
		ListNode *new_next = NULL;
		ListNode *now;
		while (pHead != NULL){
			now = pHead;
			pHead = pHead->next;

			now->next = new_next;
			new_next = now;
		}
		return new_next;
	}
};

 

原网站

版权声明
本文为[傻子是小傲娇]所创,转载请带上原文链接,感谢
https://blog.csdn.net/love_phoebe/article/details/99310417