Merge Sorted Lists & Remove Nth Node from End of List

Source: Internet
Author: User

1. Merge two sorted lists

Merge Sorted Lists

Merge sorted linked lists and return it as a new list. The new list should is made by splicing together the nodes of the first of the lists.

2. Delete the nth element of the list

Remove Nth Node from End of List

Given A linked list, remove the nth node from the end of the list and return its head.

For example,

   n = 2.   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n would always be valid.
Try to do the in one pass.

Two questions together in a project test

1  Packagecom.rust.datastruct;2 3  Public classmergetwosortedlists {4      Public StaticListNode mergetwosortedlists (listnode L1, ListNode L2) {5ListNode R1 =NewListNode (0);6ListNode res =R1;7ListNode t1 = L1;//such assignment operations in Java, the L1 operation is equivalent to the T1 operation8ListNode t2 =L2;9          while(T1! =NULL&& T2! =NULL){Ten             if(T1.val <=t2.val) { OneR1.next =T1; AT1 =T1.next; -}Else { -R1.next =T2; thet2 =T2.next; -             } -R1 =R1.next; -         } +         if(T1! =NULL) { -R1.next =T1; +         } A         if(T2! =NULL) { atR1.next =T2; -         } -res =Res.next; -         returnRes; -     } -     /** in      * @paramHead -      * @paramN to      * @returnListNode +      */ -      Public StaticListNode Removenthfromend (ListNode head,intN) { the         if(n = = 0)returnhead; *ListNode Fakenode =head; $         intCount = 0;Panax Notoginseng          while(Fakenode! =NULL) { -Fakenode =Fakenode.next; thecount++; +         } AFakenode =head; the         if(n = =count) { +Head =Head.next; -             returnhead; $}Else { $              for(inti = 0; I < count; i++) { -                 if(i + n + 1==count) { - System.out.println (fakenode.val); theListNode cut =FakeNode.next.next; -Fakenode.next =cut;Wuyicount--; the                     Continue; -                 } WuFakenode =Fakenode.next; -             } About         } $         returnhead; -     } -      -      Public Static voidMain (String args[]) { AListNode L1 =NewListNode (0); +ListNode L2 =NewListNode (1); theListNode P1 =L1; -ListNode P2 =L2; $         /*Initial the list*/ the          for(inti = 2; I <= 10; i++) { the             if(i%2 = = 0) { theP1.next =NewListNode (i); theP1 =P1.next; -}Else { inP2.next =NewListNode (i); theP2 =P2.next; the             } About         } theP1 =L1; theP2 =L2; theSYSTEM.OUT.PRINTLN ("Input List L1 and L2"); +ShowData (L1, L2);//After SHOW,L1 and L2 value didn ' t change! -System.out.println ("mergetwolists (L1, L2)--"); theListNode res =mergetwosortedlists (L1, L2);Bayi         /**test mergetwosortedlists start ************/ the          while(Res.next! =NULL) {//Res is destroyed theSystem.out.print (res.val + "\ T"); -res =Res.next; -         } the System.out.println (res.val); theSystem.out.println ("after merge"); the         /**End ***********************************/ the         /**test removenthfromend start **************/ - ShowData (L1, L2); the         //Use L2 to test the         intn = 1; theL2 =removenthfromend (l2,n);94 ShowData (L1, L2); the         /**End ***********************************/ the     } the     /**98 * Print the ListNode About      * @paramL1 ListNode -      * @paramL2 ListNode101      */102      Public Static voidShowData (listnode L1, ListNode L2) {103System.out.println ("L1--");104          while(L1.next! =NULL) { theSystem.out.print (l1.val + "\ T");106L1 =L1.next;107         }108 System.out.println (l1.val);109SYSTEM.OUT.PRINTLN ("L2--"); the          while(L2.next! =NULL) {111System.out.print (l2.val + "\ T"); theL2 =L2.next;113         } the System.out.println (l2.val); theSystem.out.println ("/************************************/"); the     }117 }118 119 //Definition for singly-linked list. - classListNode {121     intVal;122 ListNode Next;123ListNode (intX) {val =x;}124}

Output:

Input List L1 and L2
L1--
0246810
L2--
13579
/************************************/
Mergetwolists (L1, L2)--
012345678910
After merge
L1--
012345678910
L2--
12345678910
/************************************/
9
L1--
0123456789
L2--
123456789
/************************************/

Merge Sorted Lists & Remove Nth Node from End of List

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.