コード例 #1
0
		//Print a LinkedList to check the result
		public static void PrintList(ListNode head){
			Console.WriteLine(" ");
			while(head!=null){
				Console.Write (head.val + "->");
				head = head.next;
			}
		}
コード例 #2
0
		//Reverse LinkedList
		public static ListNode ReverseList(ListNode head){
			if(head==null) return null;
			ListNode prev = null;
			ListNode curr = head;
			ListNode next;
			while (curr != null) {
				next = curr.next;
				curr.next = prev;
				prev = curr;
				curr = next;
			}
			return prev; //after the while loop, curr must be null, so return prev!
		}
コード例 #3
0
		public static void Main (string[] args)
		{
			ListNode l1 = new ListNode (1);
			ListNode l2 = new ListNode (2);
			ListNode l3 = new ListNode (3);
			ListNode l4 = new ListNode (4);
			ListNode l5 = new ListNode (5);
			l1.next = l2;
			l2.next = l3;
			l3.next = l4;
			l4.next = l5;
			Solution.PrintList (l1);
			Solution.PrintList (Solution.ReverseList (l1));

		}
コード例 #4
0
		public ListNode(int v){
			this.val = v;
			this.next = null;
		}
コード例 #5
0
 public ListNode(int val = 0, ListNode next = null)
 {
     this.val  = val;
     this.next = next;
 }