给定一个单链表的头结点pHead(该头节点是有值的,比如在下图,它的val是1),长度为n,反转该链表后,返回新链表的表头。
数据范围:
要求:空间复杂度
,时间复杂度
。
如当输入链表{1,2,3}时,
经反转后,原链表变为{3,2,1},所以对应的输出为{3,2,1}。
以上转换过程如下图所示:
{1,2,3}{3,2,1}{}{}空链表则输出空
两个方法,递归和循环:public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ public ListNode ReverseList (ListNode head) { // 方法1 // return reverse(null, head); // 方法2 ListNode curNode = null; while (head != null) { ListNode tempNode = head.next; head.next = curNode; curNode = head; head = tempNode; } return curNode; } private ListNode reverse(ListNode head, ListNode nextNode) { if (nextNode == null) { return head; } ListNode tempNode = nextNode.next; nextNode.next = head; return reverse(nextNode, tempNode); } }
public ListNode ReverseList (ListNode head) {
// write code here
if(head==null)
return null;
ListNode reversedHead = null;
ListNode current = head;
ListNode pre = null;
ListNode temp = null;
while(current!=null){
temp = current.next;
current.next = pre;
pre = current;
current = temp;
}
reversedHead = pre;
return reversedHead;
} import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode ReverseList (ListNode head) {
// 处理空链表或只有一个节点的情况
if (head == null || head.next == null) {
return head;
}
ListNode prev = null; // 前一个节点,初始为null
ListNode current = head; // 当前节点,从表头开始
ListNode next = null; // 下一个节点的临时存储
while (current != null) {
next = current.next; // 先保存下一个节点
current.next = prev; // 反转当前节点的指向
prev = current; // 移动prev到当前节点
current = next; // 移动current到下一个节点
}
// 反转完成后,prev成为新的表头
return prev;
}
}
//判断是否为空链表,若为空链表直接返回空链表
if(head.next==null){
return head;
}
//头结点设置为priorNode,头结点的next设为currentNode
ListNode priorNode=head;
ListNode currentNode = head.next;
//头结点的next设置为null
head.next=null;
while(currentNode.next!=null){//TODO
//链表反转前记录current的下一个结点
ListNode tempNode = currentNode.next;
//链表地址反转操作
currentNode.next=priorNode;
//进行下一次链表反转操作前,先将prior和current向后移动以为
priorNode=currentNode;
currentNode=tempNode;
}
//为最后一个结点执行反转操作
ListNode tempNode = currentNode.next;
currentNode.next=priorNode;
return currentNode; import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode ReverseList (ListNode head) {
// write code here
// 头结点为空 或者 无后继 直接返回头结点
if (head == null || head.next == null) {
return head;
}
// 定义pre、cur和next
ListNode pre = head;
ListNode cur = head.next;
ListNode next = cur.next;
// 初始先给头结点,也就是翻转后的尾结点的next置空
pre.next = null;
// 循环反转链表
while (cur != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
} import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 思路:
初始化一个新的链表节点,用于存储反转后的链表。
遍历原链表,将每个节点依次插入到新链表的头部。
返回新链表的头节点。
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode ReverseList (ListNode head) {
ListNode newHead = null;
while (head != null) {
ListNode nextNode = head.next;
head.next = newHead;
newHead = head;
head = nextNode;
}
return newHead;
}
} //采用了递归思想
public ListNode ReverseList (ListNode head) {
// write code here
if(head ==null ||head.next == null){
return head;
}
ListNode cur = ReverseList(head.next);
head.next.next = head;//节点的下一个指向了自己
head.next = null;//自己的就断开
return cur;
} public ListNode ReverseList (ListNode head) {
if(head == null) return null;//如果给的是空链表,直接返回空
if(head.next == null) return head;//如果给的链表只有一个节点,不用反转,直接返回
//如果链表至少是两个节点,才执行下面的代码
ListNode temp = head.next;//要反转的节点(从第二个节点开始)
ListNode nextNode = temp.next;//要反转的节点的下一个节点
head.next = null;//先断开头节点和要反转节点的连接,不然反转完之后会形成环
//头插法进行反转
while (temp != null){
temp.next = head;
head = temp;
temp =nextNode;
if(nextNode != null)
nextNode = nextNode.next;
}
return head;
} 时间复杂度O(n) 空间复杂度O(1)。
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; } }