{1,2,3}{3,2,1}{}{}空链表则输出空
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null)
return null;
//head为当前节点,如果当前节点为空的话,那就什么也不做,直接返回null;
ListNode pre = null;
ListNode next = null;
//当前节点是head,pre为当前节点的前一节点,next为当前节点的下一节点
//需要pre和next的目的是让当前节点从pre->head->next1->next2变成pre<-head next1->next2
//即pre让节点可以反转所指方向,但反转之后如果不用next节点保存next1节点的话,此单链表就此断开了
//所以需要用到pre和next两个节点
//1->2->3->4->5
//1<-2<-3 4->5
while(head!=null){
//做循环,如果当前节点不为空的话,始终执行此循环,此循环的目的就是让当前节点从指向next到指向pre
//如此就可以做到反转链表的效果
//先用next保存head的下一个节点的信息,保证单链表不会因为失去head节点的原next节点而就此断裂
next = head.next;
//保存完next,就可以让head从指向next变成指向pre了,代码如下
head.next = pre;
//head指向pre后,就继续依次反转下一个节点
//让pre,head,next依次向后移动一个节点,继续下一次的指针反转
pre = head;
head = next;
}
//如果head为null的时候,pre就为最后一个节点了,但是链表已经反转完毕,pre就是反转后链表的第一个节点
//直接输出pre就是我们想要得到的反转后的链表
return pre;
}
} public ListNode ReverseList(ListNode head) {
ListNode pre = null;
ListNode next = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str=null;
while((str = in.readLine()) != null){
String[] str1 = str.split("[^0-9]");
StringBuffer str2 = new StringBuffer();
str2.append("{");
for(int i=str1.length-1;i>0;i--){
if(i==1){
str2.append(str1[i]);
break;
}
str2.append(str1[i] + ",");
}
str2.append("}");
System.out.print(str2.toString());
}
}
}
/*
双指针
*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null || head.next == null)return head;
//存储t2.next 因为要将t2.next设为t1,所以需要记录
ListNode tmp = null;
//相邻的两个节点
ListNode t1 = head;
ListNode t2 = head.next;
do{
tmp = t2.next;
t2.next = t1;
t1 = t2;
t2 = tmp;
}while(t2 != null);
//头节点需要设置一下,不然在第一个节点和第二个节点会产生环
head.next = null;
return t1;
}
}
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode p1=head;
ListNode p2=head;
ListNode root=head;
if(head == null){
return null;
}
int k = 1;
while(p1.next != null){
p1=p1.next;
k++;
}
int[] m=new int[k];
for(int i=0;i<k;i++){
m[i]=p2.val;
p2=p2.next;
}
ListNode p=root;
while(k>0){
root.next=new ListNode(m[k-1]);
root=root.next;
k--;
}
return p.next;
}
} class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead == NULL || pHead->next == NULL) return pHead;
ListNode* dummy = new ListNode(-1);
dummy->next = pHead;
ListNode* cur = dummy->next;
ListNode* temp = NULL;
while(cur->next){
temp = cur->next;
cur->next = temp->next;
temp->next = dummy->next;
dummy->next = temp;
}
return dummy->next;
}
}; 双指针: class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead == NULL || pHead->next == NULL) return pHead;
ListNode* pre = NULL;
ListNode* cur = pHead;
ListNode* temp = NULL;
while(cur){
temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}; import java.util.Stack;
/**
* 反转链表:核心方法 通过栈来反转链表
*先将链表入栈,然后将链表依次出栈,出栈结点的next指向当前栈的栈顶结点
*(实现核心功能后考虑健壮性问题)
* 考虑特殊情况 :
* 1.head为空
* 2.链表长为1
* 3.链表特别长
* @author zzc
*/
public class solution { public ListNode ReverseList(ListNode head) { if(head==null){ return null; }
Stack<ListNode> stack = new Stack<ListNode>();
ListNode node=head;
while(node!=null){
stack.add(node);
node=node.next;
}
ListNode rhead=stack.peek();
while(!stack.empty()){
node=stack.pop();
if(!stack.empty()){
node.next=stack.peek();
}else{
node.next=null;
}
}
return rhead;
}
}
// 基础版
public class Solution {
public ListNode ReverseList(ListNode head) {
if (head == null) return null;
if (head.next == null) return head;
ListNode lastNode = head, curNode = head.next, nextNode = head.next.next;
lastNode.next = null;
while (curNode.next != null) {
curNode.next = lastNode;
lastNode = curNode;
curNode = nextNode;
nextNode = nextNode.next;
}
curNode.next = lastNode;
return curNode;
}
}
// 简洁版
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode last = null, next = null;
while (head != null) {
next = head.next;
head.next = last;
last = head;
head = next;
}
return last;
}
}
public class ReverseList {
//循环
public ListNode demo1(ListNode head){
if (head == null){
return null;
}
ListNode pre = null;
ListNode next;
while (head != null){
//保存head的下一个节点,防止断开链后找不到下一个节点
next = head.next;
//将head节点的下一个节点指向前一个节点pre,实现链表反转
head.next = pre;
pre = head;
head = next;
}
return pre;
}
//递归
public ListNode demo2(ListNode head){
if (head == null || head.next == null){
return head;
}
//递归出原链表尾部节点node
ListNode node = demo2(head.next);
//将原链表从后向前一个个置于原链表尾部后面,并将原链表置为null
(head.next).next = head;
(head.next) = null;
return node;
}
}
public ListNode ReverseList(ListNode head){
/*
头插法
*/
/*ListNode NewHead=null,Next=head,Temp=null;
if(head==null||head.next==null)
return head;
while (Next!=null){
Temp=Next.next;
Next.next=NewHead;
NewHead=Next;
Next=Temp;
}
return NewHead;*/
/*
递归解法
*/
if(head==null||head.next==null)
return head;
ListNode NewHead=ReverseList(head.next);
head.next.next=head;
head.next=null;
return NewHead;
}
/*
输入一个链表,反转链表后,输出链表的所有元素
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
//head:当前节点
public ListNode ReverseList(ListNode head) {
//先定义两个变量,分别指向head前和后
ListNode prev = null ;
ListNode last = null ;
//当前节点不为空
while ( head != null )
{
//先记录当前节点的下一个
last = head.next ;
//反转,即将当前节点的下一个节点指向前一个节点
head.next = prev ;
//当前节点反转完毕,将其作为下一个节点的前一个节点记录下来
prev = head ;
//当前节点指向下一个
head = last ;
}
//当前节点为空了,说明反转到最后了,将其前一个即新的首节点返回
return prev ;
}
}
public class Solution { public static ListNode ReverseList(ListNode head) { if(head==null) return null; ListNode reversedHead=null; ListNode current=head; ListNode tmp=null; ListNode pre=null; while(current!=null){ tmp=current.next; current.next=pre; if(tmp==null) reversedHead=current; pre=current; current=tmp; } return reversedHead; } }