反转链表
题目:
输入一个链表,反转链表后,输出新链表的表头。
代码:
package com.hlq.test;
/**
* @author helongqiang
* @date 2020/5/17 12:38
*/
/**
* 输入一个链表,反转链表后,输出新链表的表头。
*/
public class Solution {
public ListNode ReverseList(ListNode head){
if(head == null){
return null;
}
ListNode pre = null;
ListNode nex = null;
while(head.next != null){
nex = head.next;
head.next = pre;
pre = head;
head = nex;
}
head.next = pre;
return head;
}
}