Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Stacks
- {
- class Program
- {
- static void Main(string[] args)
- {
- }
- static Stack<Node<T>> pushListToStack<T>(Node<T> list, ref int counter)
- {
- Stack<Node<T>> stack = new Stack<Node<T>>();
- counter = 0;
- while (list != null)
- {
- stack.push(list);
- list = list.getNext();
- counter++;
- }
- return stack;
- }
- static void separateLists<T>(ref Node<T> list1, ref Node<T> list2) {
- int length1 = 0,
- length2 = 0;
- Stack<Node<T>> s1 = pushListToStack<T>(list1, ref length1),
- s2 = pushListToStack<T>(list2, ref length2);
- list1 = null;
- list2 = null;
- // while we're still in the shared part
- while (!s1.isEmpty() && !s2.isEmpty()
- && s1.top().Equals(s2.top())) {
- // if the first list is longer than the second
- if (length1 >= length2) {
- list1 = s1.pop();
- }
- else {
- list2 = s2.pop();
- }
- }
- if (!s1.isEmpty()) {
- if (length1 < length2) {
- list1 = s1.pop();
- list1.setNext(null);
- }
- // get all the left nodes in the stack of list1
- while (!s1.isEmpty())
- list1 = s1.pop();
- }
- if (!s2.isEmpty()) {
- if (length1 >= length2) {
- list2 = s2.pop();
- list2.setNext(null);
- }
- // get all the left nodes in the stack of list1
- while (!s2.isEmpty())
- list2 = s2.pop();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement