Moves all elements in the list that are odd to the front of an even-numbered node, guaranteeing that the order of the odd numbers is constant, and that the order of the even numbers is the same.

Source: Internet
Author: User

2. Move all elements in the list that are odd to the front of an even-numbered node and ensure that the order of the odd numbers is constant and the order of the even numbers is the same.

Example:

Order of the linked list after the order of the Swap list

4→5→3→1→2 ==> 5→3→1→4→2

1 ==> 1 (linked list contains only one element)

2→1 ==>1→2

==> (the linked list is empty)

C + +:

The linked list node is defined as:

struct Node {

struct node *next;

int value;

};

struct node *swap (struct node *list);

Java:

The linked list node is defined as:

Class Node {

Public Node Next;

public int Value

}

Node Swap (node list)

Attention points and requirements are as follows:

1. The swap function requires manipulation of the pointer/reference to the node (no new linked list nodes must be created)

2. Do not use any library function/api, if you want to use similar function, please do it yourself

3. do not convert the linked list to other types of data structures and then exchange them, such as arrays

Package offer;/** * Tree structure * @author cxx * */public class ListNode {int val; ListNode next = null; ListNode (int val) {this.val = val;} ListNode () {}}package offer;/** * moves all elements in the list to an odd number in front of an even-numbered node and guarantees that the order of odd numbers is constant and the order of the even numbers is the same. * @author CXX * */public class Main {public static void main (string[] args) {ListNode listNde1 = new ListNode (4); ListNode listNde2 = new ListNode (5); ListNode listNde3 = new ListNode (3); ListNode listNde4 = new ListNode (1); ListNode listNde5 = new ListNode (2); listnde1.next = Listnde2;listnde2.next = Listnde3;listnde3.next = ListNde4; Listnde4.next = LISTNDE5; Main main = new main (); ListNode Listnde = Main.swap (listNde1);//53142while (Listnde! = null) {System.out.println (listnde.val); Listnde = Listnde.next;}} /** * Link List element Interchange method odd-numbered nodes move to the front of an even-numbered node * 1. Find the tail element and determine the end point of the program * 1. Find the first odd element and put the even number before the element to the tail * * * * @param listnode * @return */ Public ListNode Swap (ListNode listnode) {if (ListNode = = null) {return null;} if (Listnode.next = = null) {return listnode;} ListNode end = listnode;//Tail element listnodePrev = null;//pointer moves the previous node ListNode Curr = listnode;//pointer moves the current node/** * loop, finds the tail node */while (end.next! = null) {end = End.next;} ListNode newend = end;//The new tail node, constantly storing the received even elements. Place the even number of the first odd number before the end of the chain while (curr.val% 2 = = 0 && Curr! = end) {Newend.next = Curr;curr = Curr.next;newend.next.next = Null;newend = Newend.next;} The element is an odd if (curr.val% 2 = 0) {/* header node for the first odd */listnode = Curr;while (curr! = end) {if ((curr.val)% 2! = 0) {//Odd prev = C Urr;curr = Curr.next;} else {//to point the pre to the latter node Prev.next = Curr.next;curr.next = Null;newend.next = curr;//The current even to the end of the chain//end of the chain Newend = curr;//continue to Judge Cur R = Prev.next;}}} else {//According to the theory, at this point the Curr can be only the tail node, with while (curr.val% 2 = = 0 && Curr! = end) to determine prev = Curr;} Special handling of the tail node if ((end.val)% 2 = = 0) {Prev.next = End.next;end.next = Null;newend.next = end;} return listnode;}}

  

Moves all elements in the list that are odd to the front of an even-numbered node, guaranteeing that the order of the odd numbers is constant, and that the order of the even numbers is the same.

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.