Sword refers to the offer face question 16 reverse linked list (recursive and non-recursive), face test 17 merge two sorted list (recursive) __ Jian refers to an offer

Source: Internet
Author: User
face Test 16: Reverse linked list (recursive and non-recursive)

Enter the head node of a linked list, reverse the list and output the first node of the linked list.

Idea 1: Define three pointers, which are the current nodes to be reversed, the previous node and the last node.

Idea 2: Use recursion. First find the reciprocal two nodes reverse, and then forward.

Here is the Java implementation:

class listnode{int value;
	ListNode Next;
	public ListNode (int x) {value = x; } public class Reverselist {private ListNode reverselist (ListNode headnode) {if (Headnode = null) {SYSTEM.OUT.PRI
			Ntln ("Input header node is empty");
		return null;  ListNode Nownode = headnode;//The node that is currently being reversed listnode Prenode = null;//the previous node ListNode nextnode = null;//after a node ListNode
			Revhead = null;//reversed head node while (Nownode!= null) {if (Nownode.next = null) {//Inverted header node is before the end node revhead = Nownode;
			} nextnode = Nownode.next;
			Nownode.next = Prenode;
			Prenode = Nownode;
		Nownode = NextNode;
	return revhead;
			} private void Print (ListNode headnode) {if (Headnode = null) {System.out.println ("Input header node is empty");
		Return
			while (Headnode!= null) {System.out.println (headnode.value);
		Headnode = Headnode.next; }//Recursive method (not very well understood) private ListNode Digui (ListNode headnode) {//Judgment list is empty or there is only one element in the list if (Headnode = null | | headnode.ne
		XT = = null) {return headnode; }else{ListNode REvhead = Digui (Headnode.next);//First reverse the following list HeadNode.next.next = headnode;//reverse headnode.next = null;
		return revhead;
		} public static void Main (string[] args) {ListNode one = new ListNode (1);
		ListNode two = new ListNode (2);
		ListNode three = new ListNode (3);
		One.next = two;
		Two.next = three;
		Reverselist test = new Reverselist ();
		System.out.println ("before reversal:");
		Test.print (one);
		System.out.println ("Reversed:");
		Test.print (Test.reverselist (one));
	Test.print (Test.digui (one));
 }
}
face Test 17: Merge two sorted list (recursive)

Enter two ascending lists, merging the two lists and making the new list still incremented.

Thought: Recursion. Each time the first node of the two list is compared, the small one is presented, and then its next equals the recursive return value.

The Java implementation is as follows:

public class Merge {private ListNode merge (ListNode L1,listnode L2) {if (L1 = null) {return L2;
		}else if (L2 = null) {return L1;
		} ListNode mergehead = null;
			if (L1.value < l2.value) {mergehead = L1;
		Mergehead.next = Merge (L1.next, L2);
			}else{mergehead = L2;
		Mergehead.next = Merge (L1, l2.next);
	return mergehead;
			////ListNode Create the linked list private create (int[] arr) {if (arr.length = = 0) {System.out.println ("Input array is empty");
		return null;
		} ListNode Headnode = new ListNode (arr[0]);
		ListNode p = headnode;
			for (int i= 1;i<arr.length;i++) {listnode node = new ListNode (arr[i));
			P.next = node;
		p = p.next;
	return headnode;
			}//printout private void print (ListNode headnode) {if (Headnode = null) {System.out.println ("Enter blank when printing");
		Return
			while (Headnode!= null) {System.out.println (headnode.value);
		Headnode = Headnode.next;
		} public static void Main (string[] args) {int[] a = {1,3,5,7};
	Int[] B = {2,4,6,8};	Merge test = new merge ();
		ListNode a = Test.create (a);
		ListNode B = test.create (b);
		System.out.println ("A:");
		Test.print (A);
		System.out.println ("B:");
		Test.print (B);
		System.out.println ("Merge:");
	Test.print (Test.merge (A, B));
	class listnode{int value;
	ListNode Next;
	public ListNode (int x) {value = x; }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.