反转链表
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
public static ListNode ReverseList(ListNode head) {
if(head==null)
return null;
ListNode reversedHead=null;
ListNode current=head;
ListNode tmp=null;
//循环遍历节点
while(current!=null){
// 将节点赋予temp
tmp= current;
//
current= current.next;
//将节点尾指针置为空
tmp.next=null;
//判断反转链表是否为空
if(reversedHead==null)
reversedHead=tmp;
//否则按照头插法插入
else{
tmp.next=reversedHead;
reversedHead=tmp;
}
}
//返回头节点
return reversedHead;
}
}
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
public static ListNode ReverseList(ListNode head) {
if(head==null)
return null;
ListNode reversedHead=null;
ListNode current=head;
ListNode tmp=null;
//循环遍历节点
while(current!=null){
// 将节点赋予temp
tmp= current;
//
current= current.next;
//将节点尾指针置为空
tmp.next=null;
//判断反转链表是否为空
if(reversedHead==null)
reversedHead=tmp;
//否则按照头插法插入
else{
tmp.next=reversedHead;
reversedHead=tmp;
}
}
//返回头节点
return reversedHead;
}
}
全部评论
相关推荐
点赞 评论 收藏
分享
07-08 11:55
山西大学 测试工程师 
点赞 评论 收藏
分享
06-12 19:52
吉首大学张家界学院 Python 点赞 评论 收藏
分享