Merge pull request #1414 from rbshealy/patch-1

Update and rename ListAddnFun to ListAddnFun.java
This commit is contained in:
Stepfen Shawn
2020-08-16 15:16:50 +08:00
committed by GitHub

View File

@ -33,16 +33,16 @@ package DataStructures.Lists;
LinkedList updLL1=new LinkedList(); LinkedList updLL1=new LinkedList();
updLL1.formRes(LL1,LL2,LL3,MID); updLL1.formRes(LL1,LL2,LL3,MID);
updLL1.display(); updLL1.display();
updLL1.Size(); updLL1.size();
*/ */
import java.util.*; import java.util.*;
import java.lang.*; import java.lang.*;
import java.io.*; import java.io.*;
class LinkedList {
public class LinkedList {
private class Node{ private class Node{
int data; int data;
Node next; Node next;
@ -52,9 +52,16 @@ class LinkedList {
this.next = null; this.next = null;
} }
} }
public Node head = null;
public Node tail = null; private Node head;
private int size=0; private Node tail;
private int size;
public LinkedList() {
head = null;
tail = null;
size = 0;
}
public void addLast(int data) { public void addLast(int data) {
Node newNode = new Node(data); Node newNode = new Node(data);
@ -92,6 +99,7 @@ class LinkedList {
current=current.next.next; current=current.next.next;
} }
} }
public void formLL3(LinkedList LL1) { public void formLL3(LinkedList LL1) {
Node current=LL1.head.next; Node current=LL1.head.next;
while(current.next!=null&&current.next.next!=null) { while(current.next!=null&&current.next.next!=null) {
@ -100,6 +108,7 @@ class LinkedList {
current=current.next.next; current=current.next.next;
} }
} }
public Node mid() { public Node mid() {
Node slow=this.head; Node slow=this.head;
Node fast=this.head; Node fast=this.head;
@ -109,11 +118,13 @@ class LinkedList {
} }
return slow; return slow;
} }
public Node midValue() { public Node midValue() {
int sum=this.head.data+this.tail.data; int sum=this.head.data+this.tail.data;
Node mid=new Node(sum); Node mid=new Node(sum);
return mid; return mid;
} }
public void formRes(LinkedList LL1,LinkedList LL2,LinkedList LL3,Node MID) { public void formRes(LinkedList LL1,LinkedList LL2,LinkedList LL3,Node MID) {
Node LL1mid=LL1.mid(); Node LL1mid=LL1.mid();
Node currentLL1=LL1.head; Node currentLL1=LL1.head;
@ -135,7 +146,8 @@ class LinkedList {
currentLL1=currentLL1.next; currentLL1=currentLL1.next;
} }
} }
public void Size() {
public void size() {
System.out.println(this.size); System.out.println(this.size);
} }
} }