单链表排序

闲来无事,写了个单链表排序。本人算法很渣,欢迎各种拍砖,各种优化,各种新实现。

以下算法是基于冒泡排序的思想。两两比较将最大的放到最后(冒泡排序是将最小的升到最前)

 

 

单链表节点类SingleLinkNode.java

package link;
public class SingleLinkNode {
	private int data;
	private SingleLinkNode nextNode;
	public SingleLinkNode(int data, SingleLinkNode nextNode) {
		this.data = data;
		this.nextNode = nextNode;
	}
	public int getData() {
		return data;
	}
	public void setData(int data) {
		this.data = data;
	}
	public SingleLinkNode getNextNode() {
		return nextNode;
	}
	public void setNextNode(SingleLinkNode nextNode) {
		this.nextNode = nextNode;
	}
}

 

单链表类SingleLinkList.java

package link;
import java.util.Random;
public class SingleLinkList {
	// the header node of the single link list
	private SingleLinkNode headerNode;
	
	public SingleLinkNode getHeaderNode() {
		return headerNode;
	}
	// construct the single link list which contains 10 nodes
	public SingleLinkList() {
		headerNode=new SingleLinkNode(new Random().nextInt(100),null);
		SingleLinkNode currentNode=headerNode;
		for(int i=0;i<9;i++){
			SingleLinkNode nextNode=new 
			SingleLinkNode(new Random().nextInt(100),null);
			currentNode.setNextNode(nextNode);
			currentNode=nextNode;
		}
	}
	public String toString(){
		String result="";
		SingleLinkNode currentNode=headerNode;
		while(currentNode!=null){
			result+=currentNode.getData()+"-->";
			currentNode=currentNode.getNextNode();
		}
		result+="null";
		return result;
	}
}

 

 排序算法类Utils.java

package link;
public class Utils {
	// base on bubble sort
	public static SingleLinkList sort(SingleLinkList singleLinkList) {
		SingleLinkNode headerNode=singleLinkList.getHeaderNode();
		SingleLinkNode currentNode=headerNode;
		SingleLinkNode nextNode=currentNode.getNextNode();
		if(nextNode==null){
			return singleLinkList;
		}
		SingleLinkNode lastNode=null;
		
		int tempData=-1;
		// if the last node is the second node,the loop is over
		while(headerNode.getNextNode()!=lastNode){
			// check whether the next node is the last node.
			// if yes, then ship this loop.
			if(nextNode==lastNode){
				lastNode=currentNode;
				currentNode=singleLinkList.getHeaderNode();
				nextNode=currentNode.getNextNode();
				continue;
			}
			if(currentNode.getData()>nextNode.getData()){
				// exchange the data
				tempData=currentNode.getData();
				currentNode.setData(nextNode.getData());
				nextNode.setData(tempData);
			}
			currentNode=currentNode.getNextNode();
			nextNode=currentNode.getNextNode();
		}
		return singleLinkList;
	}
}

 

 

 

 

### 单链表排序算法实现代码 对于单链表排序问题,一种常见的做法是将链表中的值提取到一个数组中进行排序,然后再将排序后的值重新填回链表中。这种方式简单且易于实现,适用于大多数情况。 以下是基于 C++、Java 和 Python 的单链表排序实现: #### C++ 实现 ```cpp struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(nullptr) {} }; class Solution { public: ListNode* sortInList(ListNode* head) { std::vector<int> nums; ListNode* p = head; // 遍历链表,将节点值加入数组 while (p != nullptr) { nums.push_back(p->val); p = p->next; } p = head; // 对数组元素排序 std::sort(nums.begin(), nums.end()); // 遍历数组,将排序后的值依次加入链表 for (int i = 0; i < nums.size(); ++i) { p->val = nums[i]; p = p->next; } return head; } }; ``` #### Java 实现 ```java class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public class Solution { public ListNode sortInList(ListNode head) { List<Integer> nums = new ArrayList<>(); ListNode p = head; // 遍历链表,将节点值加入列表 while (p != null) { nums.add(p.val); p = p.next; } p = head; // 对列表元素排序 Collections.sort(nums); // 遍历列表,将排序后的值依次加入链表 for (int num : nums) { p.val = num; p = p.next; } return head; } } ``` #### Python 实现 ```python class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def sort_in_list(head): nums = [] p = head # 遍历链表,将节点值加入列表 while p: nums.append(p.val) p = p.next p = head # 对列表元素排序 nums.sort() # 遍历列表,将排序后的值依次加入链表 for num in nums: p.val = num p = p.next return head ``` ### 算法分析 - **时间复杂度**:O(n log n),其中 `n` 是链表的长度。排序算法的时间复杂度主要由排序函数(如 `std::sort`、`Collections.sort` 或 `list.sort()`)决定。 - **空间复杂度**:O(n),因为需要额外的存储空间来保存链表的值。 这种实现方式虽然在空间上不是最优解,但对于大多数应用场景已经足够高效,并且代码逻辑清晰易懂[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值