剑指Offer----反转链表
2017-07-24 17:24
369 查看
输入一个链表,反转链表后,输出链表的所有元素。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null)
return null;
ListNode newHead = null;
ListNode pNode = head;
ListNode pPrev = null;
while(pNode!=null){
ListNode pNext = pNode.next;
if(pNext==null)
newHead = pNode;
pNode.next = pPrev;
pPrev = pNode;
pNode = pNext;
}
return newHead;
}
}public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null||head.next==null)return head;
ListNode pReverseNode=ReverseList(head.next);
head.next.next=head;
head.next=null;
return pReverseNode;
}
}
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null)
return null;
ListNode newHead = null;
ListNode pNode = head;
ListNode pPrev = null;
while(pNode!=null){
ListNode pNext = pNode.next;
if(pNext==null)
newHead = pNode;
pNode.next = pPrev;
pPrev = pNode;
pNode = pNext;
}
return newHead;
}
}public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null||head.next==null)return head;
ListNode pReverseNode=ReverseList(head.next);
head.next.next=head;
head.next=null;
return pReverseNode;
}
}
相关文章推荐
- 反转链表--《剑指offer》
- 《剑指offer》003-反转链表
- 剑指offer——链表的递归反转打印
- 剑指offer-反转链表
- 剑指offer:反转链表
- JAVA实现链表的反转(《剑指offer》)
- 剑指Offer(Java版):反转链表
- 《剑指offer》面试题16—反转链表
- 剑指offer--- 反转链表
- 剑指Offer系列---(18)反转链表(递归与非递归实现)
- 《剑指offer》学习之--反转链表与合并两个排序链表
- 剑指Offer面试题16反转链表(递归和非递归),面试题17合并两个排序的链表(递归)
- 剑指offer——反转链表
- 《剑指offer》编程题java实现(十三):反转链表
- 《剑指Offer》学习笔记--面试题16:反转链表
- 反转链表 --剑指offer
- 【剑指offer系列】 反转链表___16
- 《剑指offer》牛客网java题解-反转链表
- 《剑指Offer》读书笔记--面试题16:反转链表
- 剑指offer——反转链表 Java版