package com.test;
import java.util.ArrayList;
import java.util.List;
/**
* Create by yangshunfan
* 2018/3/29 23:31
* 翻转链表
*/
public class Test35 {
public ListNode reverse(ListNode head) {
// write your code here
if (head == null) {
return null;
}
ListNode cur = head.next;
ListNode pre = head;
ListNode temp;
while (cur != null) {
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
//这里先将head.next设为空,再见pre赋值给head
head.next = null;
head = pre;
return head;
}
}
class ListNode{
int val;
ListNode next;
public ListNode(int val) {
this.val = val;
this.next = null;
}
}
本文介绍了一种翻转链表的算法实现方法,通过迭代的方式改变链表中每个节点的指向,最终达到整个链表反转的目的。文章提供了一个具体的Java实现示例。
515

被折叠的 条评论
为什么被折叠?



