Java实现单链表反转

本文主要介绍单链表反转的两种方法,记录如下:

1.

package com.leetcode;

public class ListReverse {

	public static void main(String[] args) {
		Node node1 = new Node(1);
		Node node2 = new Node(2);
		Node node3 = new Node(3);
		Node node4 = new Node(4);
		node1.next = node2;
		node2.next = node3;
		node3.next = node4;

		Node head = reverse(node1);
		while(head != null){
			System.out.println(head.val);
			head = head.next;
		}

	}

	public static Node reverse(Node head){
		Node cur = head, post = head.next;
		head.next = null;
		while(post != null){
			Node tmp = post;
			post = post.next;
			tmp.next = cur;
			cur = tmp;
		}
		return cur;
	}

	static class Node{
		int val;
		Node next;
		Node(int val){
			this.val = val;
			this.next = null;
		}
	}

}

运行结果为:

4
3
2
1

2.

package com.leetcode;

public class ListReverse {

	public static void main(String[] args) {
		Node node1 = new Node(1);
		Node node2 = new Node(2);
		Node node3 = new Node(3);
		Node node4 = new Node(4);
		node1.next = node2;
		node2.next = node3;
		node3.next = node4;

		Node head = reverse(node1);
		while(head != null){
			System.out.println(head.val);
			head = head.next;
		}

	}

	public static Node reverse(Node head){
		Node dummy = new Node(-1);
		dummy.next = head;
		Node pre = dummy, cur = head, post = head.next;
		while(post != null){
			cur.next = post.next;
			post.next = pre.next;
			pre.next = post;
			post = cur.next;
		}
		return dummy.next;
	}

	static class Node{
		int val;
		Node next;
		Node(int val){
			this.val = val;
			this.next = null;
		}
	}

}

运行结果为:

4
3
2
1
时间: 2024-07-31 15:07:54

Java实现单链表反转的相关文章

单链表反转(Singly Linked Lists in Java)

单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法 Java代码   package dsa.linkedlist; public class Node<E>{ E data; Node<E> next; } Java代码   package dsa.linkedlist; public class ReverseLinkedListRecursively { public static void main(String args[])

单链表反转java代码

据说单链表反转问题面试中经常问,而链表这个东西相对于数组的确稍微难想象,因此今天纪录一下单链表反转的代码. 1,先定义一个节点类. 1 public class Node { 2 int index; 3 Node next; 4 5 public Node(int index, Node next) { 6 this.index = index; 7 this.next = next; 8 } 9 } 2,我一共写了三种方法 (1)迭代法.先将下一节点纪录下来,然后让当前节点指向上一节点,再将

Java 实现单链表反序

//单链表反序 public class SingleLinkedListReverse { public static void main(String[] args) { Node head = new Node(0); Node temp = null; Node cur = null; for (int i = 1; i <= 10; i++) { temp = new Node(i); if (i == 1) { head.setNext(temp); } else { cur.set

单链表反转问题

单链表反转问题 基本问题 如何将单链表反转? 算法实现 /** * * Description: 单链表反转. * * @param head * @return ListNode */ public static ListNode reverseList(ListNode head) { if (head == null) { return head; } ListNode prev = null; ListNode current = head; ListNode next = null;

单链表反转的2种方法

1 public class ReverseDemo { 2 3 /** 4 * 单链表反转的两种方法 5 */ 6 public static void main(String[] args) { 7 Node head =new Node("a"); 8 Node node2=new Node("b"); 9 Node node3=new Node("c"); 10 head.setNext(node2); 11 node2.setNext(

链表 单链表反转

思路1:O(n^2). “狸猫换太子”,不进行改动链表结构,只首尾交换len/2次.但是在本函数中用到了定位函数,定位函数实际上是遍历了一遍整个链表,所以综合效率很低,达到O(n^2). //单链表反转(O(n^2)) void reverseList(Node* Head) { int count = numOfNodes(Head); //首尾交换 for(int i=1; i<=count/2; i++) { Node* p1 = locateNodeI(Head, i); Node* p

java实现单链表

前面已经介绍了java如何实现顺序链表:http://www.cnblogs.com/lixiaolun/p/4643664.html 接下来,我们开始学习java实现单链表. 单链表类 package linklist; public class LinkList { class Element { public Object value=null; private Element next=null; } private Element header = null;//头结点 /** * 初

单链表反转C语言实现

单链表的反转可以使用循环,也可以使用递归的方式 1.循环反转单链表 循环的方法中,使用pre指向前一个结点,cur指向当前结点,每次把cur->next指向pre即可. 代码: # include <iostream> # include <cstdlib> using namespace std; struct linkNode { int val; linkNode *next; linkNode(int x):val(x),next(NULL){} }; linkNod

单链表反转python实现

单链表的反转可以使用循环,也可以使用递归的方式 1.循环反转单链表 循环的方法中,使用pre指向前一个结点,cur指向当前结点,每次把cur->next指向pre即可. 代码: class ListNode: def __init__(self,x): self.val=x; self.next=None; def nonrecurse(head): #循环的方法反转链表 if head is None or head.next is None: return head; pre=None; c