Skip to content

williamdonald/wj2se

Repository files navigation

	/**
	 * <pre>
	 * the functionality is to link reversely between pre and head. 
	 * and when head is null, return the pre as the head
	 * </pre>
	 * @param pre
	 * @param head
	 * @return 
	 */
	static Node reverseByRecursive(Node pre, Node head){
		//return reversed head
		if(head == null){
			return pre;
		}
		//for next phase.
		Node next = head.next;
		System.out.println("link from [ "+head.value + "] to ["+(pre==null?"null":pre.value)+"]");
		//Do reverse.
		head.next = pre;
		
		return reverseByRecursive(head, next);
	}
	
	static Node reverseLinkedListByIterative(Node head){
		//state variables
		Node pre = null;
		Node next = null;
		while (head != null) {
			next = head.next;
			head.next = pre;
			pre = head;
			head = next;
		}

		return pre;
	}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages