日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java链表奇数倒序偶数顺序_将链表中的所有元素为奇数的节点移到元素为偶数节点的前面,并保证奇数之间顺序不变,偶数之间顺序不变。...

發(fā)布時間:2024/1/8 编程问答 27 豆豆

2.將鏈表中的所有元素為奇數(shù)的節(jié)點移到元素為偶數(shù)節(jié)點的前面,并保證奇數(shù)之間順序不變,偶數(shù)之間順序不變。

示例:

交換前鏈表的順序???????????? 交換后鏈表的順序

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

1 ==> 1?????????????????? (鏈表僅含一個元素)

2→1 ==>1→2

==>???????? (鏈表為空)

C/C++:

鏈表節(jié)點定義為:

struct node {

struct node *next;

int value;

};

struct node *swap(struct node *list);

Java:

鏈表節(jié)點定義為:

class Node {

public Node next;

public int value

}

Node swap(Node list)

注意點和要求如下:

1. swap函數(shù)要求對節(jié)點的指針/引用進(jìn)行操作(不得創(chuàng)建任何新的鏈表節(jié)點)

2. 不得使用任何庫函數(shù)/API,如需使用類似功能, 請自行實現(xiàn)

3.不得將鏈表轉(zhuǎn)化為其他類型數(shù)據(jù)結(jié)構(gòu)再進(jìn)行交換,如數(shù)組等

package offer;

/**

* 樹結(jié)構(gòu)

* @author cxx

*

*/

public class ListNode {

int val;

ListNode next = null;

ListNode(int val) {

this.val = val;

}

ListNode() {

}

}

package offer;

/**

* 將鏈表中的所有元素為奇數(shù)的節(jié)點移到元素為偶數(shù)節(jié)點的前面,并保證奇數(shù)之間順序不變,偶數(shù)之間順序不變。

* @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);

// 53142

while (listNde != null) {

System.out.println(listNde.val);

listNde = listNde.next;

}

}

/**

* 鏈表元素的交換方法 奇數(shù)的節(jié)點移到元素為偶數(shù)節(jié)點的前面

* 1.查找尾元素,確定程序的結(jié)束點

* 1.找到第一個奇數(shù)元素,并將該元素之前的偶數(shù)放到尾部

*

*

*

* @param listNode

* @return

*/

public ListNode swap(ListNode listNode) {

if(listNode == null){

return null;

}

if(listNode.next == null){

return listNode;

}

ListNode end = listNode;//尾元素

ListNode prev = null;//指針移動的前一個節(jié)點

ListNode curr = listNode;//指針移動的當(dāng)前節(jié)點

/**

* 循環(huán),查找尾節(jié)點

*/

while (end.next != null) {

end = end.next;

}

ListNode newEnd = end;// 新的尾節(jié)點,不斷的存放接收的偶數(shù)元素。

// 將第一個奇數(shù)前的偶數(shù)放到鏈尾

while (curr.val % 2 == 0 && curr != end) {

newEnd.next = curr;

curr = curr.next;

newEnd.next.next = null;

newEnd = newEnd.next;

}

// 元素是奇數(shù)

if (curr.val % 2 != 0) {

/* 頭結(jié)點為第一個奇數(shù) */

listNode = curr;

while (curr != end) {

if ((curr.val) % 2 != 0) {//奇數(shù)

prev = curr;

curr = curr.next;

} else {

// 將pre指向后一個節(jié)點

prev.next = curr.next;

curr.next = null;

newEnd.next = curr;// 將當(dāng)前的偶數(shù)放到鏈尾

// 鏈尾后移

newEnd = curr;

// 繼續(xù)判斷

curr = prev.next;

}

}

} else {

//根據(jù)理論,此時curr只有可能是尾節(jié)點,有while (curr.val % 2 == 0 && curr != end) 判斷

prev = curr;

}

// 尾節(jié)點的特殊處理

if ((end.val) % 2 == 0) {

prev.next = end.next;

end.next = null;

newEnd.next = end;

}

return listNode;

}

}

總結(jié)

以上是生活随笔為你收集整理的java链表奇数倒序偶数顺序_将链表中的所有元素为奇数的节点移到元素为偶数节点的前面,并保证奇数之间顺序不变,偶数之间顺序不变。...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。