链表(一)反转单链表+打印两个链表公共部分

反转单链表:要求链表长度为N,时间复杂度要求为O(n),额外空间复杂度为O(1)

思路1:先将head.next的节点保存下来,然后修改head的指向,(指向前一个节点)。然后保存当前head节点(为下一节点的前结点),将小标[i]移动位置到下一个节点继续操作。

//直接反转
	public static Node reverse(Node head) {
		Node pre=null;
		Node next=null;
		while(head!=null)
		{
			next=head.next;
			head.next=pre;
			pre=head;
			head=next;//下移一位
		}
		return pre;
	}

思路2:递归实现(不停的传送至下一节点,默认为剩下的链表反转完之后在连接会head)

//递归反转
	public static Node reverse2(Node head) {
		if(head==null||head.next==null) {return head;}
//一定要先判断head是否为空,可能第一个传进来的参数head就为空
		Node next=head.next;//记录接下来传递的节点
		head.next=null;//断开联系
		//除去head剩下的链表反转
		Node h2=reverse2(next);
		//当后面的都反转了之后,连接到刚刚的head
		next.next=head;
		return h2;
	}

打印链表:

//打印链表
	public static void printLinkList(Node head) {
		while(head!=null) {
			System.out.println(""+head.data);
			head=head.next;
		}
		System.out.println();
	} 

主函数:

	//测试主函数
	public static void main(String[] args) {
		Node head1 = new Node(1);
		head1.next = new Node(2);
		head1.next.next = new Node(3);
		head1.next.next.next = new Node(4);
		printLinkList(head1);
		head1 = reverse2(head1);
		printLinkList(head1);

	}

全部代码:


public class ReverseList {
	//链表结构
	public static class Node{
		public int data;
		public Node next;
		public Node(int d){
			this.data=d;
		}
	}
	//直接反转
	public static Node reverse(Node head) {
		Node pre=null;
		Node next=null;
		while(head!=null)
		{
			next=head.next;
			head.next=pre;
			pre=head;
			head=next;//下移一位
		}
		return pre;
	}
	//递归反转
	public static Node reverse2(Node head) {
		if(head.next==null) {return head;}
		Node next=head.next;//记录接下来传递的节点
		head.next=null;//断开联系
		//除去head剩下的链表反转
		Node h2=reverse2(next);
		//当后面的都反转了之后,连接到刚刚的head
		next.next=head;
		return h2;
	}
	//打印链表
	public static void printLinkList(Node head) {
		while(head!=null) {
			System.out.println(""+head.data);
			head=head.next;
		}
		System.out.println();
	} 
	//测试主函数
	public static void main(String[] args) {
		Node head1 = new Node(1);
		head1.next = new Node(2);
		head1.next.next = new Node(3);
		head1.next.next.next = new Node(4);
		printLinkList(head1);
		head1 = reverse2(head1);
		printLinkList(head1);

	}
	

打印两个有序链表的公共部分:

直接遍历...

public static void common (Node head1,Node head2) {
		while(head1!=null&&head2!=null) {
			if(head1.value>head2.value)
				head2=head2.next;
			else if(head1.value<head2.value)
				head1=head1.next;
			else if(head1.value==head2.value)
				{System.out.print(head1.value+" ");
				head1=head1.next;
				head2=head2.next;
				}
		}
		System.out.println();
	}

完整代码

package Node;

import Node.ReverseList.Node;

public class CommentList {
	public static class Node{
		int value;
		Node next;
		Node(int a){
			this.value=a;
		}
	}
	
	public static void common (Node head1,Node head2) {
		while(head1!=null&&head2!=null) {
			if(head1.value>head2.value)
				head2=head2.next;
			else if(head1.value<head2.value)
				head1=head1.next;
			else if(head1.value==head2.value)
				{System.out.print(head1.value+" ");
				head1=head1.next;
				head2=head2.next;
				}
		}
		System.out.println();
	}

	public static void main(String[] args) {
		Node node1 = new Node(2);
		node1.next = new Node(3);
		node1.next.next = new Node(5);
		node1.next.next.next = new Node(6);

		Node node2 = new Node(1);
		node2.next = new Node(2);
		node2.next.next = new Node(5);
		node2.next.next.next = new Node(7);
		node2.next.next.next.next = new Node(8);

		printLinkList(node1);
		printLinkList(node2);
		common(node1, node2);

	}

	public static void printLinkList(Node head) {
		while(head!=null) {
			System.out.println(""+head.value);
			head=head.next;
		}
		System.out.println();
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值