public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null) {
return null;
}
ListNode node = head.next;
head.next = null;
while(node != null) {
ListNode nextNode = node.next;
node.next = head;
head = node;
node = nextNode;
}
return head;
}
}
class Solution { public: ListNode* ReverseList(ListNode* pHead) { if(!pHead) return NULL; ListNode *p=pHead,*temp; ListNode *Head=(ListNode *)malloc(sizeof(ListNode)); Head->next=NULL; while(p) temp=p->next,p->next=Head->next, Head->next=p,p=temp; return Head->next; } };
/* 只需要完成逆置链表函数 struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; */ class Solution { public: ListNode* ReverseList(ListNode* pHead) { if(pHead==nullptr) return pHead; ListNode *tmp,*cur=pHead,*pre=nullptr; while(cur){ tmp=cur->next; cur->next=pre; pre=cur; cur=tmp; } return pre; } };
public class Solution { ListNode root, tp = new ListNode(0); public ListNode ReverseList(ListNode head) { root = tp; dfs(head); tp.next = null; return root.next; } public void dfs(ListNode head) { if(head != null) { dfs(head.next); tp.next = head; tp = tp.next; } } }
class Solution { public: ListNode* ReverseList(ListNode* pHead) { if(!pHead || !pHead->next) return pHead; if(!pHead->next->next) { pHead->next->next = pHead; pHead = NULL; return pHead->next; } ListNode* pre = pHead; ListNode* p = pHead->next; ListNode* post = p->next; pre->next = NULL; while(post) { p->next = pre; pre = p; p = post; post = post->next; } p->next = pre; return p; } };
public ListNode ReverseList(ListNode head) { if (head == null || head.next == null) return head; ListNode pre = null, next = null; while (head.next != null) { next = head.next; head.next = pre; pre = head; head = next; } head.next = pre; return head; }
通过一次遍历得到链表的长度len,然后根据链表长度len来确定后续遍历的次数,每取出一个元素len-1
public ListNode ReverseList(ListNode head) { if (head == null) return null; ListNode temp = head; ListNode res = new ListNode(0); int len = 0; while (temp.next != null){ temp = temp.next; len++; } res.val = temp.val; ListNode result = res; while (len > 0){ temp = head; for (int i = 0; i < len-1; i++ ){ temp = temp.next; } res.next = new ListNode(temp.val); res = res.next; len--; } return result; }
/* 只需要完成逆置链表函数 struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; */ //依次遍历链表节点,每遍历一个节点就逆置一个节点 class Solution { public: ListNode* ReverseList(ListNode* pHead) { if(!pHead) return nullptr; ListNode* pHeadNew = nullptr; while(pHead) { ListNode* pTemp = pHead->next; pHead->next = pHeadNew; pHeadNew = pHead; pHead = pTemp; } return pHeadNew; } };
/*
只需要完成逆置链表函数
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
/**
* 每次从旧链表的头部取出节点插入到新链表中
*/
ListNode* ReverseList(ListNode* pHead) {
if (nullptr == pHead || nullptr == pHead->next) {
return pHead;
}
ListNode *pNewHead = nullptr;
ListNode *pTemp = nullptr;
while (pHead) {
pTemp = pHead;
pHead = pHead->next;
// 头插法
pTemp->next = pNewHead;
pNewHead = pTemp;
}
return pNewHead;
}
};
class Solution { public: ListNode* ReverseList(ListNode* pHead) { if (pHead==NULL||pHead->next==NULL) return pHead; ListNode *p,*newHead,*oldHead; for (oldHead=pHead->next,newHead=pHead;oldHead!=NULL;) { p=oldHead; oldHead=oldHead->next; p->next=newHead; if (newHead==pHead) newHead->next=NULL; newHead=p; } return newHead; } };