链表问题4——反转单向链表

题目

实现反转单向链表的函数


要求

如果链表长度为N,时间复杂度要求为O(N),额外空间复杂度要求为O(1).


源码

public class Node{
	public int value;
	public Node next;
	public Node(int data){
		this.value=data;
	} 
}

public Node reverseList(Node head){
	Node pre=null;
	Node next=null;
	while(head!=null){
		next=head.next;
		head.next=pre;
		pre=head;
		head=next;
	}
	return pre;
	
}
发布了43 篇原创文章 · 获赞 21 · 访问量 4919

猜你喜欢

转载自blog.csdn.net/flying_1314/article/details/103837486