给定一个单向链表L(N0,N1,N2,N3……),在不改变node值得情况下,来编程实现对链表重新排列 ,使得排序后的链表为(N0,Nn,n1,Nn-1,n2,Nn-2……)。
来源:互联网 发布:软件编程网站 编辑:程序博客网 时间:2024/05/17 22:25
package Test;
//给定一个单向链表L(N0,N1,N2,N3……),在不改变node值得情况下,来编程实现对链表重新排列
//使得排序后的链表为(N0,Nn,n1,Nn-1,n2,Nn-2……)。
//思路:用快慢指针截断前后两段链表,后半段链表反转,交叉拼接前半段和后半段链表
public class linkTest {
public static void main(String[] args) {
Node head=new Node(0);
Node tmp=new Node();
tmp=null;
Node cur=new Node();
cur=null;
for(int i=1;i<10;i++)
{
tmp=new Node(i);
if(i==1)
head.setNext(tmp);
else
cur.setNext(tmp);
cur = tmp;
}
Node h = head;
while (null != h) {
System.out.print(h.getValue() + " ");
h = h.getNext();
}
}
//重排链表
public static Node linkSort(Node head) {
if (head == null)
return null; // 快慢指针寻找中间节点,一个指针一步,一个指针两步,当两步的指针走到了最后,1步的指针刚好走到中间
Node first = new Node();
first=head;
Node second = new Node();
second=head;
//查找中间节点
while (second.next != null) {
second = second.next;
first = first.next;
if (second.next != null) {//first移动一步,second移动两步, null.next报错,故要分开,
second = second.next;
}
} // 截取后半段链表
second = first.next;//这是first指向中间结点
first.next = null; // 反转后半段链表
Node cur = second; Node rhead = null;
while(cur!=null){
Node front = cur;
cur =cur.next;
front.next = rhead;
rhead = front;
}
//拼接两段链表
Node nhead = head;
while(nhead!=null&&rhead!=null){
Node temp =nhead.next;
Node rtemp = rhead.next;
nhead.next =rhead;
rhead.next = temp;
nhead = temp;
rhead =rtemp;
}
if(rhead!=null) //前后半段一样长的处理
nhead.next= rhead;
return head;
}
}
//给定一个单向链表L(N0,N1,N2,N3……),在不改变node值得情况下,来编程实现对链表重新排列
//使得排序后的链表为(N0,Nn,n1,Nn-1,n2,Nn-2……)。
//思路:用快慢指针截断前后两段链表,后半段链表反转,交叉拼接前半段和后半段链表
public class linkTest {
public static void main(String[] args) {
Node head=new Node(0);
Node tmp=new Node();
tmp=null;
Node cur=new Node();
cur=null;
for(int i=1;i<10;i++)
{
tmp=new Node(i);
if(i==1)
head.setNext(tmp);
else
cur.setNext(tmp);
cur = tmp;
}
Node h = head;
while (null != h) {
System.out.print(h.getValue() + " ");
h = h.getNext();
}
}
//重排链表
public static Node linkSort(Node head) {
if (head == null)
return null; // 快慢指针寻找中间节点,一个指针一步,一个指针两步,当两步的指针走到了最后,1步的指针刚好走到中间
Node first = new Node();
first=head;
Node second = new Node();
second=head;
//查找中间节点
while (second.next != null) {
second = second.next;
first = first.next;
if (second.next != null) {//first移动一步,second移动两步, null.next报错,故要分开,
second = second.next;
}
} // 截取后半段链表
second = first.next;//这是first指向中间结点
first.next = null; // 反转后半段链表
Node cur = second; Node rhead = null;
while(cur!=null){
Node front = cur;
cur =cur.next;
front.next = rhead;
rhead = front;
}
//拼接两段链表
Node nhead = head;
while(nhead!=null&&rhead!=null){
Node temp =nhead.next;
Node rtemp = rhead.next;
nhead.next =rhead;
rhead.next = temp;
nhead = temp;
rhead =rtemp;
}
if(rhead!=null) //前后半段一样长的处理
nhead.next= rhead;
return head;
}
}
0 0
- 给定一个单向链表L(N0,N1,N2,N3……),在不改变node值得情况下,来编程实现对链表重新排列 ,使得排序后的链表为(N0,Nn,n1,Nn-1,n2,Nn-2……)。
- 想让一个数拆成 y=2^n1+2^n2+……,这个算法实现心得
- 给定一个正整数n,将其分成m段,每段为n1,n2,...,nm,求怎么划分使得n1*n2*...*nm最大
- Linux下的C语言编程——查找介于n1与n2(0<n1<n2<32768)之间所有满足下列条件的整数
- C语言编程练习——查找介于n1与n2(0<n1<n2<32768)之间所有满足下列条件的整数:
- /查找介于n1与n2(0<n1<n2<32768)之间所有满足下列条件的整数
- 将长度为N的钢管截成长为n1,n2,n3各若干根,求所有可能的组合
- 二叉树n0 = n2 + 1
- 非空二叉树的一个有趣的性质:n0 = n2 + 1
- 把内表 itab1 的 n1 到 n2 行内容附加到 itab2 内表中去.
- 把内表 itab1 的 n1 到 n2 行内容附加到 itab2 内表中去.
- 查找介于n1与n2(0<n1<n2<32768)之间所有满足下列条件的整数: (1)该数的十进制表示中有且仅有两个相同的数字位; (2)该数是素数。
- 查找介于n1与n2(0<n1<n2<32768)之间所有满足下列条件的整数: (1)该数的十进制表示中有且仅有两个相同的数字位; (2)该数是素数
- 查找介于n1与n2(0<n1<n2<32768)之间所有满足下列条件的整数: (1)该数的十进制表示中有且仅有两个相同的数字位; (2)该数是素数
- 查找介于n1与n2(0<n1<n2<32768)之间所有满足下列条件的整数: (1)该数的十进制表示中有且仅有两个相同的数字位; (2)该数是素数。
- 查找介于n1与n2(0<n1<n2<32768)之间所有满足下列条件的整数: (1)该数的十进制表示中有且仅有两个相同的数字位; (2)该数是素数。
- SUBSTRB(c1,n1[,n2])
- 声明两个变量:int n1=10,n2=20;要求将两个变量交换,最后输出n1为20,n2为10。扩展(*):不使用第三个变量如何交换?
- 探索RadioGroup下可否添加子Layout
- Android、java解析json工具类
- 线太细时用UIView或UIImageView都会导致闪烁 但用UILabel就好了
- 2012蓝桥杯赛题题解
- 代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧
- 给定一个单向链表L(N0,N1,N2,N3……),在不改变node值得情况下,来编程实现对链表重新排列 ,使得排序后的链表为(N0,Nn,n1,Nn-1,n2,Nn-2……)。
- Java对MySQL数据库进行连接、查询和修改
- 在类的成员函数中调用delete this
- git资料
- linux内核链表分析
- C++ Standard Library/std/STL/ATL/WTL的区别与联系
- 详细介绍Linux重定向的使用
- String,StringBuffer和StringBuild 区别
- socket笔记